-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolutioncpp.cpp
More file actions
35 lines (30 loc) · 933 Bytes
/
Solutioncpp.cpp
File metadata and controls
35 lines (30 loc) · 933 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
//The whole idea behind insertion sort is to sort the array in parts i.e , first sort till index 1, then index 2 ....and so on.
//Lets see the implementation
#include <iostream>
using namespace std;
void swap(int arr[],int first,int second){
int temp = arr[first];
arr[first]=arr[second];
arr[second]=temp;
}
void insertionsort(int arr[],int n){
for(int i=0; i<n-1; i++){
for(int j=i+1; j>0; j--){
if(arr[j]<arr[j-1]){
swap(arr,j,j-1);//calling the swap function
}else{
break; //if arr[j-1] is not less than j then no need to check previous element because they are already sorted
}
}
}
//printing the sorted array
for(int index=0; index<n; index++){
cout<<arr[index]<<" ";
}
}
int main() {
int arr[] = { 5,3,7,1,8,6,9 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionsort(arr,n);
return 0;
}