forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
42 lines (35 loc) · 691 Bytes
/
InsertionSort.cpp
File metadata and controls
42 lines (35 loc) · 691 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
#include <iostream>
#include <vector>
using namespace std;
void insertionSort(vector<int> &vector) {
for (uint32_t index = 1; index < vector.size(); index++)
{
int key = vector[index];
int i = index - 1;
while (i >= 0 && vector[i] > key)
{
vector[i+1] = vector[i];
i--;
}
vector[i+1] = key;
}
}
void showVector(vector<int> vector)
{
for (uint32_t i = 0; i < vector.size(); ++i)
{
cout << vector[i] << ", ";
}
cout << "\n";
}
int main()
{
vector<int> vector;
for (uint32_t i = 0; i < 10; ++i)
{
vector.push_back(rand() % 100);
}
showVector(vector);
insertionSort(vector);
showVector(vector);
}