-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_Sort.c
More file actions
61 lines (55 loc) · 1.15 KB
/
Copy pathQuick_Sort.c
File metadata and controls
61 lines (55 loc) · 1.15 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
/*
Quick Sort follow divide and conquer technique for sorting
Best case : O(nlogn)
Medium case : O(nlogn)
Worst Case : O(n^2)
*/
#include <stdio.h>
//to find and place the pivot in correct position
int partition(int arr[],int l,int h)
{
int pivot = arr[l] , i=l , j=h;
while(i<j)
{
do
{
i++;
} while (pivot>=arr[i]);
do
{
j--;
} while (pivot<arr[j]);
if(i<j)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
arr[l] = arr[j];
arr[j] = pivot;
return j;
}
//to divide the array at pivot and then sort the parts formed by same procedure
void QuickSort(int arr[],int l,int h)
{
if(l<h)
{
int j = partition(arr,l,h);
QuickSort(arr,l,j);
QuickSort(arr,j+1,h);
}
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0 ; i<n ; ++i)
scanf("%d",&arr[i]);
QuickSort(arr,0,n);
//array sorted
for(int i=0 ; i<n ; ++i)
printf("%d ",arr[i]);
return 0;
}