-
Notifications
You must be signed in to change notification settings - Fork 1
/
quick_sort.c
67 lines (58 loc) · 1.03 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
66
67
#include<stdio.h>
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int array[],int low,int high)
{
int i=low;
int j=high;
int pivot=low;
while(i<j)
{
while(array[i] <= array[j])
i++;
while(array[j] > array[pivot])
j--;
if(i<j)
{
swap(&array[i],&array[j]);
}
}
swap(&array[j],&array[pivot]);
return j;
}
void quicksort(int array[],int low,int high)
{
if(low<high)
{
int j=partition(array,low,high);
quicksort(array,low,j-1);
quicksort(array,j+1,high);
}
}
void display(int n, int array[])
{
int i;
printf("After Sorting: \n");
for(i=0;i<n;i++)
{
printf(" %d",array[i]);
}
}
void main()
{
int n,i;
int array[10];
printf("How many numbers?\n");
scanf("%d",&n);
printf("Enter the elements: \n");
for(i=0;i<n;i++)
{
scanf(" %d",&array[i]);
}
quicksort(array,0,n-1);
display(n,array);
}