-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick.cpp
More file actions
36 lines (36 loc) · 737 Bytes
/
quick.cpp
File metadata and controls
36 lines (36 loc) · 737 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
35
36
#include<iostream>
using namespace std;
void swap(int *a, int *b){
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int Partition(int *A, int start, int end){
int pivot = A[end];
int partitionIndex = start;
for (int i = start; i < end; i++)
{
if (A[i] <= pivot){
swap(A[i], A[partitionIndex]);
partitionIndex++;
}
}
swap(A[partitionIndex], A[end]);
return partitionIndex;
}
void QuickSort(int *A, int start, int end){
if (start < end){
int partitionIndex = Partition(A, start, end);
QuickSort(A,start, partitionIndex-1);
QuickSort(A, partitionIndex + 1, end);
}
}
int main(){
int A[] = { 43, 6, 99, 31, 3, 4, 1 ,0};
QuickSort(A, 0, 7);
for (int i = 0; i < 8; i++)
{
cout << A[i] << ""<<",";
}
getchar();
}