-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.cpp
More file actions
71 lines (62 loc) · 2.08 KB
/
Copy pathinsertionSort.cpp
File metadata and controls
71 lines (62 loc) · 2.08 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// InsertionSort.cpp
// https://www.youtube.com/watch?v=8oJS1BMKE64
#include <iostream>
// Quick Sort
// This function takes the last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot
int partition (int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1); // index of smaller element
for (int j = low; j < high; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] >= pivot) {
i++; // increment index of smaller element
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);
// Recursively sort elements before partition and after partition
quickSort(arr, low, pi - 1); //Left of pivot(partition index)
quickSort(arr, pi + 1, high);//Right of pivot
}
}
// Insertion Sort
void insertionSort(int arr[], int size)
{
int i, j, key;
// iterate through the array, starting at position 1
for (i = 1; i < size; i++)
{
// key is the current item, we need to determine if it should move
key = arr[i];
// j is the previous index and will allow us to shift items forward
j = i - 1;
// Shift everything from arr[0..i-1] that is greater than the key
// No if's req'd as this guarantees everything ahead is already sorted
while (j >= 0 && arr[j] < key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
std::cout << '\n';
}
int main()
{
int numbers[] = { 9, 0, 5, 3, 8, 1, 4, 6, 7, 10, 2 };
int size = sizeof(numbers) / sizeof(numbers[0]);
quickSort(numbers, 0, size - 1);
print(numbers, size);
return 0;
}