-
Notifications
You must be signed in to change notification settings - Fork 1
/
Insertion_Sort.cpp
63 lines (44 loc) · 786 Bytes
/
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
52
53
54
55
56
57
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter n How many elements you want in your array\n";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cout<<"Enter the element:"<<i<<" \n";
cin>>a[i];
}
cout<<"Your Array is:";
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
cout<<"\n";
int *Insertion_Sort(int arr[],int n);
Insertion_Sort(a, n);
cout<<"Sorted Array is:";
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
cout<<"\n";
return 0;
}
int *Insertion_Sort(int arr[], int n){
int key;
for(int i=1; i<n; i++){
key = arr[i];
for(int j=i-1; j>=0; j--){
if(key < arr[j]){
arr[j+1] = arr[j];
if(j == 0){
arr[j] = key;
}
}
if(key > arr[j]){
arr[j+1] = key;
break;
}
}
}
return arr;
}