-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickSort.c
More file actions
43 lines (37 loc) · 708 Bytes
/
Copy pathquickSort.c
File metadata and controls
43 lines (37 loc) · 708 Bytes
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
#include<stdio.h>
void quickSort(int arr[], int p, int r);
int partition(int arr[], int p, int r);
void swap(int *, int *);
int main(){
int arr[]={9,8,7,6,4,3,2,0,1,5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
for(int i =0; i<n; i++){
printf("%d",arr[i]);
}
return 0;
}
void quickSort(int arr[], int p, int r){
if(p<r){
int q = partition(arr,p,r);
quickSort(arr,p,q-1);
quickSort(arr,q+1,r);
}
}
int partition(int arr[], int p, int r){
int key = arr[r];
int i= p-1;
for(int j =p; j<=r-1; j++){
if(arr[j]<=key){
i++;
swap(&arr[i],&arr[j]);
}
}
swap(&arr[i+1],&arr[r]);
return (i+1);
}
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}