-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
50 lines (38 loc) · 858 Bytes
/
insertion_sort.cpp
File metadata and controls
50 lines (38 loc) · 858 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
#include <bits/stdc++.h>
using namespace std;
void InsertionSort(int arr[], int n){
int i = 1;
int lv, temp;
while(i < n)
{
lv = i - 1;
temp = arr[i];
while(lv >= 0)
{
if(arr[lv] > temp)
arr[lv+1] = arr[lv];
else
{ arr[lv+1] = temp;
break;
}
lv = lv - 1;
}
if(lv == -1)
arr[0] = temp;
i = i + 1;
}
}
int main()
{
clock_t start, endc;
int n = 1000;
int a[1000];
for(int i = 0; i < n; i++)
a[i] = rand()%1000;
start=clock();
InsertionSort(a, n);
endc=clock();
double time_taken = (endc - start) / double(CLOCKS_PER_SEC);
cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
cout << " sec " << endl;
}