-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
63 lines (59 loc) · 1.17 KB
/
Copy pathquickSort.cpp
File metadata and controls
63 lines (59 loc) · 1.17 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
/* Quick Sort */
#include <iostream>
using namespace std;
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void quickSort(int* arr, int front, int end)
{
if (arr != NULL && front < end)
{
int pivot = arr[end];
int insertPos = front - 1; // record where the pivot should insert
int j = front;
// Partition
while (j <= end - 1)
{
if (arr[j] < pivot)
{
insertPos++;
if (insertPos != j)
{
swap(&arr[insertPos], &arr[j]);
}
}
j++;
}
if (++insertPos != end)
{
swap(&arr[insertPos], &arr[end]);
}
//if(front > end)
// cout << "front: " << front << " insertPos - 1: " << insertPos - 1 << "insertPos + 1: " << insertPos + 1 << endl;
quickSort(arr, front, insertPos - 1);
quickSort(arr, insertPos + 1, end);
}
}
int main()
{
int n = 9;
int arr[] = { 9, 4, 1, 6, 7, 3, 8, 2, 5 };
//int n = 1;
//int arr[] = { 9 };
//int n = 0;
//int* arr = NULL;
//int n = 2;
//int arr[] = { 9, 2 };
//int n = 5;
//int arr[] = { 9, 2, 2, 2, 5 };
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}