-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathquickSort.cpp
More file actions
51 lines (48 loc) · 1.23 KB
/
quickSort.cpp
File metadata and controls
51 lines (48 loc) · 1.23 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
#include <iostream>
using namespace std;
int partitionArray(int input[], int start, int end)
{
// Chose pivot
int pivot = input[start];
// Count elements smaller than pivot and swap
int count = 0;
for (int i = start + 1; i <= end; i++)
{
if (input[i] <= pivot)
count++;
}
int pivotIndex = start + count;
int temp = input[start];
input[start] = input[pivotIndex];
input[pivotIndex] = temp;
// ensure left half contains elements smaller than pivot // and right half larger
int i = start, j = end;
while (i < pivotIndex && j > pivotIndex)
{
while (input[i] <= pivot)
i++;
while (input[j] > pivot)
j--;
if (i < pivotIndex && j > pivotIndex)
{
int temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
}
return pivotIndex;
}
void quickSort(int input[], int start, int end)
{
if (start >= end)
return;
int pivotIndex = partitionArray(input, start, end);
quickSort(input, start, pivotIndex - 1);
quickSort(input, pivotIndex + 1, end);
}
void quickSort(int input[], int n)
{
quickSort(input, 0, n - 1);
}