-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
46 lines (46 loc) · 969 Bytes
/
InsertionSort.cpp
File metadata and controls
46 lines (46 loc) · 969 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
#include<iostream>
#include<stdlib.h>
#include<process.h>
#include<time.h>
using namespace std;
void insertionSort(int a[], int size) {
int i, j, num;
for(j=1; j<size; j++) {
num = a[j];
i=j-1;
while(i>=0 && a[i]>num) {
a[i+1] = a[i];
i--;
}
a[i+1] = num;
}
}
int main() {
int a[100000], size=100000, ch, i;
cout<<"Insertion Sort\nEnter your Choice:\n1. Best Case\n2. Worst Case\n3. Average Case\n4. Exit\t";
cin>>ch;
switch(ch) {
case 1: for(i=0; i<100000; i++) {
a[i] = i+1;
}
break;
case 2: for(i=100000; i>0; i--) {
a[100000-i] = i;
}
break;
case 3: srand(time(NULL));
for(i=0; i<100000; i++) {
a[i] = rand() % 100000 + 1;
}
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice!\n";
break;
}
clock_t start, end;
start = clock();
insertionSort(a, size);
end = clock();
cout<<"Time Taken: "<<(double)(end-start)/CLK_TCK;
}