-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.c
More file actions
54 lines (52 loc) · 1.03 KB
/
Copy pathquick_sort.c
File metadata and controls
54 lines (52 loc) · 1.03 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
#include<stdio.h>
void printArray(int *A,int n)
{
for(int i=0;i<n;i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int partion(int *A,int low,int high)
{
int pivot=A[low];
int i=low+1; //used to find the largest element from the array;
int j=high; //used to find the samllest element from the array;
do{
while(A[i]<=pivot)
{
i++;
}
while(A[j]>pivot)
{
j--;
}
if(i<j)
{
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}while(i<j);
int temp=A[low];
A[low]=A[j];
A[j]=temp;
return j;
}
void quickSort(int *A,int low,int high)
{
if(low<high)
{
int partionIndex=partion(A,low,high);
quickSort(A,low,partionIndex-1);
quickSort(A,partionIndex+1,high);
}
}
int main()
{
int A[ ]={7,11,2,9,17,4};
int n=(sizeof(A))/(sizeof(A[0]));
printArray(A,n);//Array before sorting
quickSort(A,0,n-1);
printArray(A,n);//Array after sorting
}