Skip to content

Commit e231e9f

Browse files
Create insertion sort implementation
Please accept it
1 parent 5f79ed8 commit e231e9f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
void insertionSort(int arr[], int n)
4+
{
5+
int i, key, j;
6+
for (i = 1; i < n; i++)
7+
{
8+
key = arr[i];
9+
j = i - 1;
10+
while (j >= 0 && arr[j] > key)
11+
{
12+
arr[j + 1] = arr[j];
13+
j = j - 1;
14+
}
15+
arr[j + 1] = key;
16+
}
17+
}
18+
void printArray(int arr[], int n)
19+
{
20+
int i;
21+
for (i = 0; i < n; i++)
22+
cout << arr[i] << " ";
23+
cout << endl;
24+
}
25+
int main()
26+
{
27+
int arr[] = { 12, 11, 13, 5, 6 };
28+
int N = sizeof(arr) / sizeof(arr[0]);
29+
30+
insertionSort(arr, N);
31+
printArray(arr, N);
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)