forked from sambhav2612/SortingAlgorithms.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion-sort.cpp
47 lines (35 loc) · 878 Bytes
/
insertion-sort.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
47
# include "includeAll.h"
using namespace std;
void sort (int array[], int size) {
int value = 0, hole = 0;
for (int i = 1; i < size; ++i) {
value = array[i];
hole = i;
while (hole >0 & array[hole-1] > value) {
array[hole] = array[hole-1];
hole = hole - 1;
}
array[hole] = value;
cout << "Iteration #" << i+1 << ":" ;
for (int k = 0; k < size; ++k)
cout << array[k];
cout << endl;
}
}
int main () {
int size = 0, array[100] = { 0 };
cout << endl << "Enter Size: ";
cin >> size;
cout << endl << "Enter elements: " << endl;
for (int i = 0; i < size; ++i)
cin >> array[i];
cout << endl << "Array before sorting: " << endl;
for (int i = 0; i < size; ++i)
cout << array[i];
cout << endl;
sort (array, size);
cout << endl << "Array after sorting: " << endl;
for (int i = 0; i < size; ++i)
cout << array[i];
return 0;
}