-
Notifications
You must be signed in to change notification settings - Fork 4
/
QuickSort.cpp
51 lines (45 loc) · 876 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
#include <stdio.h>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
void swap(int *p, int *q)
{
int temp= *p;
*p = *q;
*q= temp;
}
int partition(int a[], int low, int high)
{
int pivot= a[low];
int left=low+1,right=high;
while(1)
{
while(a[left]<=pivot && left<=right)
left++;
while(a[right]>=pivot && left<=right)
right--;
if(left<right)
swap(&a[left],&a[right]);
else
break;
}
swap(&a[low],&a[right]);
return right;
}
void quicksort(int a[],int low, int high)
{
if(low<high)
{
int t=partition(a,low,high);
quicksort(a,low,t-1);
quicksort(a,t+1,high);
}
}
int main()
{
int a[]={3,2,5,4,78,47,8,453,46};
quicksort(a,0,8);
for(int i=0; i<9; i++)
cout << a[i] << " ";
return 0;
}