-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
40 lines (34 loc) · 903 Bytes
/
InsertionSort.cpp
File metadata and controls
40 lines (34 loc) · 903 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
/**
* Author: Hemant Tripathi
*/
#include<iostream>
using namespace std;
#define MAXSIZE 8
int main() {
cout << "Starting Insertion Sort program" << endl;
int dataArray[] = {25, 57, 48, 37, 12, 92, 86, 33};
for (int i=1; i<MAXSIZE; i++) {
for(int j=i; j > 0; j--) {
cout << "Comparison between "<<dataArray[j]<<" and "<<dataArray[j-1] << endl;
if(dataArray[j] < dataArray[j-1]) {
//Swap the data of i and j position
int tmp = dataArray[j];
dataArray[j] = dataArray[j-1];
dataArray[j-1] = tmp;
} else {
break;
}
}
cout << "After next iteration, array : ";
for(int x=0; x < MAXSIZE; x++) {
cout << "\t" << dataArray[x];
}
cout << endl;
}
cout << "############## Result ###################"<<endl;
cout << "After Insertion Sort operation, sorted array is: ";
for(int i=0; i < MAXSIZE; i++) {
cout << "\t" << dataArray[i];
}
cout << endl;
}