Insertion sort... Please help!
So for an assignment I'm supposed to have my insertion sort program take numbers from a file (in this case infile.txt) and sort them using insertion sort then output the array.
This is what I currently have:
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin("infile.txt");
int a[6], length, number, i;
for(int j=1;j<length;j++)
{
number=a[j];
i=j-1;
while(a[i]>number && i>=0)
{
a[i+1]=a[i];
i--;
}
a[i+1]=number;
}
cout << a[i] << "\n";
return 0;
}
When the infile.txt file lists the numbers:
5
6
98
7
5
6
1
8
6
4
However, as an output I am just getting the number 13... Does anyone know why this would be and how I would go about fixing it?
THANK YOU SO MUCH!!!!!!!!!!!