-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
49 lines (39 loc) · 792 Bytes
/
InsertionSort.cpp
File metadata and controls
49 lines (39 loc) · 792 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
//
// InsertionSort.cpp
// DataStructures
//
// Created by Suraj on 05/06/20.
// Copyright © 2020 Suraj. All rights reserved.
//
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n)
{
int key;
for (int i = 1; i < n; i++)
{
key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
arr[j] = key;
j--;
}
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { 312, 221, 54, 15, 2, 3, 77, 125, 34};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}