-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
34 lines (27 loc) · 869 Bytes
/
InsertionSort.cpp
File metadata and controls
34 lines (27 loc) · 869 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
#pragma once
#include <vector>
#include "InsertionSort.h"
void InsertionSort::Sort(vector<int>& v)
{
// (N - 1) passes from second position to the end of the array.
for (unsigned i = 1; i <= (v.size() - 1); ++i)
{
// Remove the current pass starting value from the array.
// This creates the empty space that the elements will be
// shuffled into.
int temp = v[i];
unsigned j;
// Walk backwards from the empty space and move larger
// objects upwards until either the end of the array
// or a smaller element is encountered.
for (j = i; j > 0 ; --j) {
if (temp > v[j - 1])
break;
// The elements from 0 to i - 1 have already been sorted so
// its safe to shuffle them down one position.
v[j] = v[j-1];
}
// Fill the empty space with the current pass value.
v[j] = temp;
}
}