-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion.cpp
38 lines (36 loc) · 901 Bytes
/
insertion.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
#include <bits/stdc++.h>
using namespace std;
void insSort(int ar[], int size){
int i, j, key;
for (i = 1; i < size; i++){
key = ar[i];
j = i - 1;
while (j >= 0 && ar[j] > key){
ar[j + 1] = ar[j];
j = j - 1;
}
ar[j + 1] = key;
}
}
void print(int ar[], int size){
for (int i = 0; i < size; i++){
cout << ar[i] << " ";
}
cout << endl;
}
int main(){
int len;
cout << "Enter the length of the array : ";
cin >> len;
cout << endl;
int a[len];
cout << "Enter the elements of the array :" << endl;
for (int i = 0; i < len; i++){
cin >> a[i];
}
int n = sizeof(a) / sizeof(a[0]);
insSort(a, n);
cout << "The array after insertion sort is :" << endl;
print(a, n);
cout << "This program is made by Jayant Singh of ECE branch with Scholar No. 21U01041";
}