-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsertion_sort.cpp
44 lines (34 loc) · 913 Bytes
/
insertion_sort.cpp
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
#include <iostream>
#include <vector>
using namespace std;
// just a helper function to print the vector
void printVector(vector<int> v) {
for(int i = 0, len = (int) v.size(); i < len; i++) {
cout << v[i] << " ";
}
cout << endl;
}
void isort(vector<int> mylist) {
for(int i = 1; i < (int) mylist.size(); i++){
int val = mylist[i];
int hole = i;
while(hole > 0 && val < mylist[hole - 1]) {
mylist[hole] = mylist[hole - 1];
hole--;
}
mylist[hole] = val;
printVector(mylist);
}
}
int main(void)
{
// create and populate vector
vector<int> v;
int array[] = {7, 3, 8, 6, 5, 1};
v.insert(v.end(), array, array+6);
// printing vector
cout << "Printing vector: " << endl;
printVector(v);
cout << "Insertion sorting: " << endl;
isort(v);
}