-
Notifications
You must be signed in to change notification settings - Fork 54
/
quickSort.cpp
61 lines (54 loc) · 859 Bytes
/
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
61
#include<iostream>
using namespace std;
void swap(int arr[], int x, int y)
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
int partition(int arr[], int x, int y)
{
int pivot = x;
int low = x+1;
int high = y;
while(low<=high)
{
while(arr[pivot] > arr[low])
low++;
while(arr[pivot] < arr[high])
high--;
if(low<high)
swap(arr, low, high);
}
swap(arr, high, pivot);
return high;
}
void quick_sort(int arr[], int x, int y)
{
int p;
if(x<y)
{
p = partition(arr, x, y);
quick_sort(arr, x, p-1);
quick_sort(arr, p+1, y);
}
}
int main()
{
long n;
cout<<"Enter the total number of elements: ";
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cout<<"Enter "<<i+1<<"th element: ";
cin>>arr[i];
}
int x = 0;
int y = n-1;
quick_sort(arr, x, y);
cout<<endl;
for(int i=0; i<n; i++)
cout<<arr[i];
cout<<endl;
}