-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.cpp
More file actions
44 lines (44 loc) · 1.05 KB
/
insertionSort.cpp
File metadata and controls
44 lines (44 loc) · 1.05 KB
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
#include<iostream>
using namespace std;
void swap(int &a, int &b) {
int temp;
temp=a;
a=b;
b=temp;
}
void display(int *a, int n) {
int i;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
void insertSort(int *a, int n){
int i,j;
for(i=1;i<n;i++)
{ //It will take element at ith inedx and compare it with its previous element first and if found smaller then will it swap with previous.
for(j=i;j>0;j--) //then it will be again compared with next previous element following same procedure untill no previous element is greater than it.
{
if(a[j]<a[j-1])
swap(a[j],a[j-1]);
else
break;
}
}
}
int main(){
int n, i;
cout<<"\nEnter Number of Elements ";
cin>>n;
int a[n];
cout<<"\nEnter "<<n<<" Elements ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nBefore Sorting : ";
display(a, n) ;
insertSort(a, n) ;
cout<<"\nAfter Sorting : ";
display(a, n);
return 0;
}