forked from jake1412/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
54 lines (40 loc) · 931 Bytes
/
quick_sort.cpp
File metadata and controls
54 lines (40 loc) · 931 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
void display(int data[], int size){
for(int i=0; i<size ; i++){
cout << data[i] << " ";
}
cout << endl;
}
int partition(int data[], int low, int high){
int pivot = data[high];
int pId = low; //Partition index
for(int i=low; i < high ; i++){
if(data[i] < pivot){
swap(&data[pId], &data[i]);
pId++;
}
}
swap(&data[pId], &data[high]);
return pId;
}
void quick_sort(int data[], int low, int high){
if(low < high){
int pId = partition(data, low, high);
quick_sort(data, low, pId - 1);
quick_sort(data, pId + 1, high);
}
}
int main(){
int data[] = {11, 55, 9, 44, 26, 22, 78, 0, 12, 5};
int size = 10;
display(data, size);
quick_sort(data, 0, size-1);
display(data, size);
return 0;
}