-
Notifications
You must be signed in to change notification settings - Fork 48
/
QuickSort.c
54 lines (51 loc) · 961 Bytes
/
QuickSort.c
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
// quick sort is actually used in real world applicatins
//in this code we take the last as pivot element
#include<stdio.h>
void quicksort(int nums[],int low, int high)
{
if(low>=high)
{
return;
}
int start=low;
int end=high;
int mid=(start+end)/2;
int pivot=nums[mid];
while(start<=end)
{
while(nums[start]<pivot)
{
start++;
}
while(nums[end]>pivot)
{
end--;
}
if(start<=end)
{
int temp=nums[start];
nums[start]=nums[end];
nums[end]=temp;
start++;
end--;
}
}
quicksort(nums,low,end);
quicksort(nums,start,high);
}
int main()
{
int n;
scanf("%d",&n);
int nums[n];
int i,j;
for(i=0;i<n;i++)
{
scanf("%d",&nums[i]);
}
quicksort(nums,0,n-1);
for(i=0;i<n;i++)
{
printf("%d\n",nums[i]);
}
}