-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_sort.cpp
More file actions
62 lines (61 loc) · 981 Bytes
/
Quick_sort.cpp
File metadata and controls
62 lines (61 loc) · 981 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
55
56
57
58
59
60
61
62
#include<iostream>
#include<conio.h>
using namespace std;
int partition(int a[], int lb, int ub)
{
int pivot = a[lb];
int start = lb;
int end = ub;
while (start < end)
{
while (a[start] <= pivot)
{
start++;
}
while (a[end] > pivot)
{
end--;
}
if (start < end)
{
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
int temp = a[lb];
a[lb] = a[end];
a[end] = temp;
return end;
}
void quicksort(int a[], int lb, int ub)
{
int loc;
if (lb < ub)
{
loc = partition(a, lb, ub);
quicksort(a, lb, loc - 1);
quicksort(a, loc + 1, ub);
}
return;
}
void main()
{
int a[20], lb, ub, n;
cout << "\nEnter the size of the array: ";
cin >> n;
lb = 0;
ub = n - 1;
for (int i = 0; i < n; i++)
{
cout << "\nEnter the value of array" << i + 1<<": ";
cin >> a[i];
}
quicksort(a, lb, ub);
for (int i = 0; i < n; i++)
{
cout << a[i]<<" ";
}
_getch();
return;
}