-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
57 lines (35 loc) · 954 Bytes
/
insertion_sort.cpp
File metadata and controls
57 lines (35 loc) · 954 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Data-Structures-And-Algorithms
Here I will post my regular DSA problems
#include<iostream>
using namespace std;
// Insertion sort algorithm
void insert_sort ( int *a,int n ) {
int i ,j ,key ;
for(i=1;i<n;i++){
key= a[i]; // Assign key with a[i]
j= i-1;
while( j>=0&& a[j]>key){
a[j+1]=a[j];
j--;
}
a[j+1] = key;
}
}
int main (){
int n;
cout<<"Enter 'n' value : ";
cin>>n;
cout<<endl;
int a[n];
cout<<"Enter the array elements : "<<endl<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
insert_sort ( a,n );
cout<<endl<<"The sorted array :\n\n";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}