-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cpp
60 lines (50 loc) · 1.26 KB
/
QuickSort.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
/**
* Author: Hemant Tripathi
*/
#include <iostream>
using namespace std;
#define MAXSIZE 8
void quickSort(int x[], int lb, int ub);
int main() {
int dataArray[MAXSIZE] = {25, 57, 48, 37, 12, 92, 86, 33};
quickSort(dataArray, 0, MAXSIZE-1);
cout << endl << endl;
cout << "############################ Result ##############################" << endl;
cout << "After quicksort Operation > Array: ";
for(int i=0; i < MAXSIZE; i++) {
cout << "\t" << dataArray[i];
}
cout <<endl;
}
void quickSort(int x[], int lb, int ub) {
int a = x[lb];
int up = ub;
int down = lb;
while(down < up) {
while(x[down] <= a && down < ub) {
down++;
}
while(x[up] > a) {
up--;
}
if(down < up) {
//swap the elements of up and down positions
int temp = x[down];
x[down] = x[up];
x[up] = temp;
}
}
x[lb] = x[up];
x[up] = a;
cout << "After next iteration > Array:";
for(int i=0; i < MAXSIZE; i++) {
cout << "\t" << x[i];
}
cout <<endl;
if(up > lb) {
quickSort(x, lb, up-1);
}
if(down < ub) {
quickSort(x, up+1, ub);
}
}