-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort.c
More file actions
50 lines (50 loc) · 1.02 KB
/
Quicksort.c
File metadata and controls
50 lines (50 loc) · 1.02 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
#include<stdio.h>
#include<stdlib.h>
int traverse(int *a, int n){
for (int i = 0; i < n-1 ; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
int partition(int a[], int low , int high){
int pivot = a[low];
int i = low+1;
int j = high;
int temp;
do{
while(a[i]<= pivot){
i++;
}
while(a[j]>pivot){
j--;
}
if(i<j){
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
// now swapping j with low
temp = a[low];
a[low]= a[j];
a[j]=temp;
return j;
}
void quicksort(int a[], int low, int high){
int partitionindex ; //index of pivot
if(low<high){
partitionindex = partition(a,low,high);
quicksort(a,low,partitionindex-1);//sorting left subarray
quicksort(a,partitionindex+1, high);//sorting right subarray
}
}
int main(){
int a[]= {23,45,54,32,11,77,4,30};
int n = 8;
traverse(a,n);
printf("Elements after sorting..... \n");
quicksort(a, 0 ,n-1);
traverse(a,n);
return 0;
}