-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick sort.cpp
More file actions
70 lines (56 loc) · 1.35 KB
/
quick sort.cpp
File metadata and controls
70 lines (56 loc) · 1.35 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
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
void qSort(int num[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = num[left];
while(left < right)
{
while((num[right] >= pivot) && (left < right))
right--;
if(left != right)
{
num[left] = num[right];
left++;
}
while((num[left] <= pivot) && (left < right))
left++;
if(left != right)
{
num[right] = num[left];
right--;
}
}
num[left] = pivot;
pivot = left;
left = l_hold;
right = l_hold;
if (left < pivot)
qSort(num, left, pivot-1);
if (right > pivot)
qSort(num, pivot+1, right);
}
void quickSort(int num[], int size)
{
qSort(num, 0, size-1);
}
int main()
{
int N;
cout << "Number of elements: ";
cin >> N;
int array[N];
cout << "Enter the elements: " << endl;
for(int i = 0; i < N; i++)
{
cin >> array[i];
}
quickSort(array, N);
cout << "Sorted: " << endl;
for(int i = 0; i < N; i++)
cout << array[i] << endl;
system("pause");
return 0;
}