-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
62 lines (43 loc) · 985 Bytes
/
InsertionSort.cpp
File metadata and controls
62 lines (43 loc) · 985 Bytes
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
#include "Header.h"
int Sorting:: insertion_sortSTEPS(int arr[], int length)//mit insertion sort implementation
{
int numofsteps=0, j, k, temp;
numofsteps++; //j=1
for (j = 1; j < length; j++)
{
numofsteps = numofsteps + 4;//j < length; j++; temp = hold[j]; k = j - 1;
temp = arr[j];
k = j - 1;
while (k >= 0 && arr[k] > temp)
{
arr[k + 1] = arr[k];
k--;
numofsteps = numofsteps + 4;//
}
numofsteps = numofsteps + 3;//
arr[k + 1] = temp;
}
numofsteps++;//j < length;
return numofsteps;
}
double Sorting::insertion_sortTime(int arr[], int length)//returns the amount of time it takes too run the program
{
clock_t start, end;
start = clock();
int j, k;
int temp;
for (j = 1; j < length; j++)
{
temp = arr[j];
k = j - 1;
while (k >= 0 && arr[k] > temp)
{
arr[k + 1] = arr[k];
k--;
}
arr[k + 1] = temp;
}
end= clock();
double msecs = ((double)(end - start)) * 1000 / CLOCKS_PER_SEC;
return msecs;
}