-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathInsertionSort.cpp
46 lines (40 loc) · 916 Bytes
/
InsertionSort.cpp
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>
using namespace std;
void InsertionSort(int *arr, int size){
for (size_t i = 1; i < size; i++)
{
int temp = arr[i];
int j = i -1;
while(arr[j] > temp){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
void InsertionSortRecursive(int * arr, int size){
if(size == 0 || size == 1) return;
InsertionSortRecursive(arr,size-1);
int temp = arr[size-1];
int j = size -2;
while(arr[j] > temp){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
int main(int argc, char const *argv[])
{
int arr[] = {1,5,3,8,6,9,4};
for (size_t i = 0; i < 7; i++)
{
cout << arr[i] << " ";
}
cout << endl << endl;
InsertionSortRecursive(arr, 7);
for (size_t i = 0; i < 7; i++)
{
cout << arr[i] << " ";
}
return 0;
}