-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.c
More file actions
62 lines (53 loc) · 1.11 KB
/
QuickSort.c
File metadata and controls
62 lines (53 loc) · 1.11 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
#include<stdio.h>
int partition(int *list, int lower, int upper)
{
int pivotValue = list[lower];
int temp=0, i,j;
for(i=lower+1, j= lower+1;(i<=upper && j<=upper) ;)
{
while(list[i]<=pivotValue && i<=upper)
{
//find next value which is greater than pivotValue
++i;
}
j=i;
while(list[j]>=pivotValue && j<= upper)
{
++j;
}
if(i>upper || j>upper) {break;}
temp = list[i];
list[i]=list[j];
list[j]=temp;
++i;
++j;
}
i--;
temp= list[i];
list[i]=pivotValue;
list[lower]= temp;
return i;
}
void QuickSort(int * list, int lower, int upper)
{
if(lower >= upper)
return;
int pivot=partition(list, lower, upper);
if(upper!= lower+1)
{
QuickSort(list, lower, pivot-1);
QuickSort(list, pivot+1, upper);
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",(a+i));
QuickSort(a, 0, n-1);
for(int i=0;i<n;i++)
printf("%d ",*(a+i));
return 0;
}