-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_InsertionSort.cpp
More file actions
38 lines (33 loc) · 802 Bytes
/
Copy path08_InsertionSort.cpp
File metadata and controls
38 lines (33 loc) · 802 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
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
void insertionSort(int arr[], int s) {
int i, j;
for (i = 1; i < s; i++) {
int temp = arr[i];
j = i - 1;
while (arr[j] > temp && j > -1) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
return;
}
int main() {
int size = 8;
int arr[size] = {56, 71, 28, 98, 12, 44, 64, 35};
cout << "The unsorted Array:\n";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
insertionSort(arr, size); // Insertion Sort func call
cout << "\nAfter applying insertion sort" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}