-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
37 lines (37 loc) · 833 Bytes
/
insertion_sort.cpp
File metadata and controls
37 lines (37 loc) · 833 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
#include<iostream>
using namespace std;
int display(int *arr,int num);
int insertion_sort(int *arr,int num);
int insertion_sort(int *arr,int num){
int value,hole,i;
for(i=1;i<num;i++){
//here hole is used as a next position element
value = arr[i];
hole=i;
while(hole>0 && arr[hole-1]>value){
arr[hole]=arr[hole-1];
hole=hole-1;
}
arr[hole]=value;
}
}
int display(int *arr,int num){
for(int i=0;i<num;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main(){
int n;
cout<<"Enter number of elements in an array : "<<endl;
cin>>n;
int *arr=new int[n];
cout<<"Enter elements of array : "<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Array elements are : "<<endl;
display(arr,n);
insertion_sort(arr,n);
cout<<"Sorted elements are : "<<endl;
display(arr,n);
return 0;
}