-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
50 lines (36 loc) · 849 Bytes
/
Copy pathInsertionSort.cpp
File metadata and controls
50 lines (36 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/******************************************************************************
Zenith Kile
COMS 3213
9/5/23
*******************************************************************************/
#include <iostream>
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 print(int arr[], int n)
{
for (int a = 0; a < n; a++)
cout << arr[a] << " ";
cout << endl;
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int num = sizeof(arr) / sizeof(int);
insertionSort(arr, num);
print(arr, num);
return 0;
}