Skip to content

Commit

Permalink
[ADDED] Insertion Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
sakib412 committed Nov 13, 2021
1 parent e64b791 commit f9346ad
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pc-ch-4/insertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;

void insertionSort(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
int j = i - 1;
int key = arr[i];
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main()
{
int arr[] = {5, 3, 25, 4, 0, 34, 34, 1, 4, 4, 23, 2, 2, 3, 2};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
}

0 comments on commit f9346ad

Please sign in to comment.