forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
42 lines (34 loc) · 947 Bytes
/
insertion_sort.cpp
File metadata and controls
42 lines (34 loc) · 947 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
#include<iostream>
using namespace std;
//sort the elements in increasing order
void insertion_sort(int a[], int n){
for(int i = 1; i <= n-1; i++){
int current = a[i];
int prev = i-1;
//loop to find the right index where the element current should be inserted
while(prev>=0 and a[prev] > current){
a[prev+1] = a[prev];
prev = prev - 1;
}
a[prev+1] = current;//when prev is -1 so it will take 0th place
}
}
int main(){
cout<<"SEJAL KOTHARI(209303070)"<<endl;
cout<<"INSERTION SORT"<<endl;
int i, arr[8];
//= {-2,3,4,-1,5,-12,6,1};
int n = sizeof(arr)/sizeof(int);
// cout<<"Print number of Elements: "<<endl;
// cin>>n;
cout<<"Unsorted array: "<<endl;
for(i=0;i<n;i++){
cin>>arr[i];
}
cout<<"Sorted array: "<<endl;
insertion_sort(arr,n);
for(auto x : arr){
cout<<x<<",";
}
return 0;
}