forked from harsimrans/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.c
65 lines (60 loc) · 1.12 KB
/
quick_sort.c
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
62
63
64
65
/*****************************************
*
* Quick Sort algorithm
*
* Mohit Goyal
*
* Takes an array input and outputs the
* sorted array using quick sort algo.
*
******************************************/
#include <stdio.h>
void quicksort(int*,int,int);
int partition(int*,int,int);
int main()
{
int arr[10],size = 10,i; //array arr of size 10
printf("\nEnter values : \n");
for(i=0;i<size;i++)
{
scanf("%d",arr+i);
}
printf("\nBefore Sorting List is\n");
for(i=0;i<size;i++)
{
printf(" %d",*(arr+i));
}
quicksort(arr,0,size-1);
printf("\nAfter Sorting List is\n");
for(i=0;i<size;i++)
{
printf("%d ",*(arr + i));
}
return 0;
}
void quicksort(int *a,int start,int end)
{
int pos;
if(start < end)
{
pos = partition(a,start,end);
quicksort(a,start,pos-1);
quicksort(a,pos+1,end);
}
}
int partition(int* a,int start,int end)
{
int pivot = start,select = start+1,i = start;
while(i<=end)
{
if(a[i] < a[pivot])
{
a[i] = a[select] + a[i] - (a[select] = a[i]);
select++;
}
i++;
}
a[pivot] = a[pivot] + a[select-1] - (a[select - 1] = a[pivot]);
return (select - 1);
}