-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion-sort.cpp
51 lines (40 loc) · 1 KB
/
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
48
49
50
51
#include <iostream>
// Need n-1 iterations to sort the array
using namespace std;
void insertion_sort(int arr[], int size)
{
int key;
int j;
for (int i = 1; i < size; i++)
{
key = arr[i]; // assigning key the 2nd value in array at index 1
j = i - 1; // j become behind the i
//if j goes out of index and key greater than previous element loop terminates
while (j > -1 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key; // moving key to j + 1 position
}
cout << "Sorted Array" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << endl;
}
}
int main()
{
int size;
cout << "Entry Array size" << endl;
cin >> size;
int arr[size];
// Taking array from user
for (int i = 0; i < size; i++)
{
cout << "Enter Element " << i+1 << " :: ";
cin >> arr[i];
}
//calling insertion sort
insertion_sort(arr, size);
}