forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.cpp
More file actions
44 lines (34 loc) · 755 Bytes
/
Copy pathinsertionSort.cpp
File metadata and controls
44 lines (34 loc) · 755 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
/*
* @author : imkaka
* @date : 4/2/2019
*/
#include<iostream>
using namespace std;
void insertionSort(int arr[], int size){
int key , j;
for(int i = 1; i < size; ++i){
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void print(int* arr, int size){
for(int i = 0; i < size; ++i)
cout << arr[i] << " ";
}
int main(){
int arr[] = {10, 12, 0, 36, -4, 2, 3, -36, 20};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Before Sorting: " << endl;
print(arr,size);
cout << endl;
insertionSort(arr, size);
cout << "After Sorting: " << endl;
print(arr,size);
return 0;
}