Skip to content

Commit

Permalink
Create Insertion_Sort.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
MAYANK25402 authored Feb 10, 2024
1 parent 3aa9351 commit 724aadf
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Sorting Algorithms/Insertion_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

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

while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

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

int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, N);
printArray(arr, N);

return 0;
}

0 comments on commit 724aadf

Please sign in to comment.