Insertion Sort problems

So I have this program to do where i pull numbers from a very large data file then run it through an insertion sort 1000 times, then i do it 3 more times with more and more numbers.

my problem is, I cant seem to get the data to run through the insertion sort. I also need to time how long it takes and to solve for n. I understand the insertion sort but i really dont know how to time it. i am a beginner and i know my code isnt all right, but i do know the insertion sort is right (got it from the book). Any tips and pointers...no pun intended...would be helpful

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 #include <iostream> 
#include <fstream> 
#include <string> 
#include <ctime> 
#include <string>

using namespace std;



void insertionSort(double numbers[], int array_size);

int main() { // main open file for random.dat 1000 
    int count;
    double onethousand[1000];
    fstream inputFile ("random.dat");
    time_t now;
    time(&now);

    inputFile.open("random.data");       //Opens input file
    if(inputFile.fail())
    {
        printf("Cannot open inputFile");
        exit(1);
    }

    count = 0;
    printf("%d\n",count);
    inputFile >> onethousand[count];
    printf("%f\n",onethousand[count]);
    while(inputFile)
    {   //While loop
        count++;
        if(count < 1000)
            inputFile >> onethousand[count];
    }
    inputFile.close();  
	
    printf("%s\n", ctime(&now));    //BEFORE
    insertionSort(onethousand, 1000); //Insertion Sort 1000 numbers
    printf("%s\n", ctime(&now)); //AFTER
    for(int i = 0; i < count; i++)
        printf("%f\n",onethousand[i]);  

	
}


void insertionSort(double numbers[], int array_size)
{
  int i, j, index;
  for (i=1; i < array_size; i++)
  {
    index = numbers[i];
    j = i;
    while ((j > 0) && (numbers[j-1] > index))
    {
      numbers[j] = numbers[j-1];
      j = j - 1;
    }
    numbers[j] = index;
  }
}
Topic archived. No new replies allowed.