Skip to content

Commit

Permalink
Merge pull request vikasgond807#11 from rajpatil6355/patch-1
Browse files Browse the repository at this point in the history
insertion sort in cpp
  • Loading branch information
vikasgond807 committed Oct 11, 2022
2 parents 874aee3 + e020def commit e1a72fc
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions insertionsort
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//Insertion Sort Algorithm


#include <bits/stdc++.h>
using namespace std;
void insertionSort(int a[], int n)
{
int i, o, j;
for (i = 1; i < n; i++)
{
o = a[i];
j = i - 1;
while (j >= 0 && a[j] > o)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = o;
}
}
void printArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}

int main()
{
int arr[] = { 5,23,10,2,56,4 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
// This is code is contributed by raj patil

0 comments on commit e1a72fc

Please sign in to comment.