-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick-sort.cpp
64 lines (52 loc) · 997 Bytes
/
quick-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}
void swapNumbers(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int findPivot(int arr[], int low, int high)
{
int pivot = arr[high];
int swapIndex = low - 1;
for (int i = low; i < high; i++)
{
if (arr[i] <= pivot)
{
swapIndex++;
swapNumbers(&arr[swapIndex], &arr[i]);
}
}
swapNumbers(&arr[swapIndex + 1], &arr[high]);
return (swapIndex + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pivot = findPivot(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
int main()
{
int array[] = {8, 7, 6, 1, 0, 9, 2};
int arraySize = *(&array + 1) - array;
cout << "Input Array: ";
printArray(array, arraySize);
cout << endl;
quickSort(array, 0, arraySize - 1);
cout << "Output Array: ";
printArray(array, arraySize);
cout << endl;
return 0;
}