Skip to content

Instantly share code, notes, and snippets.

@ldclakmal
Created January 24, 2015 15:58
Show Gist options
  • Save ldclakmal/aa79dbfac46c7a52ea20 to your computer and use it in GitHub Desktop.
Save ldclakmal/aa79dbfac46c7a52ea20 to your computer and use it in GitHub Desktop.
Insertion Sort
// InsertionSort.cpp : Defines the entry point for the console application.
// input.txt file should be here.
// L.D.C.Lakmal
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
#define ARRAY_SIZE 1000
#define LINE_LENGTH ARRAY_SIZE*10
int read_input_array(char* fileName, int* inputArray);
void insertion_sort(int inputSize, int* inputArray);
void display_array(int size, int* intArrray);
int main(int argc, char* argv[])
{
int myArray[ARRAY_SIZE];
int numElements = 0;
char* fileName;
if(argc == 2){
fileName = argv[1];
}
else{
cout << "Incorrect number of arguments! Input file name is required. Number of arguments received is " << argc-1 << ".\n";
system("pause");
exit(-1);
}
numElements = read_input_array(fileName, myArray);
if(numElements < 0){
cout << "Error in reading input.\n";
system("pause");
exit(-1);
}
display_array(numElements, myArray);
insertion_sort(numElements, myArray);
display_array(numElements, myArray);
system("PAUSE");
return 0;
}
/*
* This method will read the inputs from the file specified by the parameter fileName.
* The first line of the file contains the number of elements in the array.
* The second line of the array contains the numbers.
* Parameter inputArray is the array to which the umbers will be saved.
* The method will return the number of items read in case of success. IN case of failure it will return -1.
*/
int read_input_array(char* fileName, int* inputArray){
ifstream inFile;
//Open File
inFile.open(fileName);
//Check for errors in opeing file
if (inFile.fail()) {
cerr << "unable to open file " << fileName <<" for reading\n";
inFile.close();
system("pause");
return -1;
}
int n;
char currentLine[LINE_LENGTH];
//Read first line
if(!inFile.eof())
inFile.getline(currentLine,sizeof(currentLine));
else{
cout << "Error in reading first line!\n";
inFile.close();
return -1;
}
//Convert it to number of elements
n = atoi(currentLine);
//Read second line
if(!inFile.eof())
inFile.getline(currentLine,sizeof(currentLine));
else{
cout << "Error in reading second line!\n";
inFile.close();
return -1;
}
inFile.close();
//Split line into numbers and convert them to
char* number_string;
char delimiters[] = " ,.";
number_string = strtok(currentLine, delimiters);
int i = 0;
while(number_string != NULL && i<n){
inputArray[i] = atoi(number_string);
if(inputArray[i] == 0){
cout << "Error! Invalid Input: The input " <<i+1 <<" (" << number_string <<")is either zero or not a number!\n";
return -1;
}
i++;
number_string = strtok(NULL, delimiters);
}
if(number_string != NULL){
cout <<"Warning: Some inputs ignored!\n";
}
//Check whether the required number of integers were read.
if (i<n){
cout << "Error! Required number of values not fund in line 2! n is " <<n <<" and number of values read (i) is " <<i <<endl;
return -1;
}
return n;
}
void insertion_sort(int inputSize, int* inputArray){
//Implement Insertion Sort Here
for(int i=1; i<inputSize; i++){
int key = inputArray[i];
int j = i-1;
while (j>=0 && inputArray[j]>key){
inputArray[j+1] = inputArray[j];
j = j-1;
}
inputArray[j+1] = key;
}
}
void display_array(int size, int* intArray){
cout << "Displaying Array: ";
cout << "\tArray Size: " << size << " - Elements: ";
for(int i=0; i<size; i++){
cout << intArray[i];
if(i < size-1)
cout <<", ";
else
cout <<"\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment