-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.c
More file actions
48 lines (44 loc) · 807 Bytes
/
Copy pathQuickSort.c
File metadata and controls
48 lines (44 loc) · 807 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
44
45
46
47
48
#include<stdio.h>
#include<stdlib.h>
void swap(int *a,int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partationIndex(int *str,int start,int end)
{
int pIndex,pivot;
pIndex = start-1;
pivot = str[end];
for(int i=start;i<=end-1;i++)
{
if(str[i] <= pivot)
{
pIndex++;
swap(&str[i],&str[pIndex]);
}
}
swap(&str[pIndex+1],&str[end]);
return pIndex+1;
}
void quickSort(int *str,int start,int end)
{
if(start<end)
{
int pivot = partationIndex(str,start,end);
quickSort(str,start,pivot-1);
quickSort(str,pivot+1,end);
}
}
int main()
{
int str[] = {4,5,2,7,8,10,9,6,1,3};
int len,i;
len = sizeof(str)/sizeof(str[0]);
quickSort(str,0,len-1);
for(i=0;i<len;i++)
printf("%d ",str[i]);
printf("\n");
return 0;
}