MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / insertion_sort

Function insertion_sort

CPP/sorting/insertionsort.cpp:3–17  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include"bits/stdc++.h"
2using namespace std;
3void insertion_sort(int arr[],int n){
4 for (int i = 1; i < n; i++) {
5 int temp=arr[i];
6 int j=i-1;//we are using j here because we are changing j+1 th elemnt to the temp
7 //ulta looop chalake check krenge aur shift krnge(if the element is greater than temp or not
8
9 for (;j>=0;j--) {
10 if(arr[j]>temp)
11 arr[j+1]=arr[j];
12 else
13 break;
14 }
15 arr[j+1]=temp;
16 }
17}
18int main(){
19 int arr[]={5,3,2,4,1};
20 int n=sizeof(arr)/sizeof(int);

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected