-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_sort.c
89 lines (65 loc) · 1.79 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
void print_array(int a[],int size)
{
for(int i=0;i<size;i++)
printf("%d ",a[i]);
printf("\n");
}
int partition(int list[],int low,int high)
{
int down_ptr=low,up_ptr=high,pivot=low,temp;
/* while (i<j), we loop and do the following thing
1) Repeatedly increase the down_ptr until list[down_ptr]<=list[pivot]
2) Repeatedly decrease the up_ptr until list[up_ptr]>=list[pivot]
If (down_ptr < up_ptr),we swap the corresponding elements
*/
while(down_ptr<up_ptr)
{
while (list[down_ptr] <= list[pivot] && down_ptr <= high)
down_ptr++;
while (list[up_ptr] > list[pivot] && up_ptr >= low)
up_ptr--;
if (down_ptr < up_ptr)
{
temp = list[down_ptr];
list[down_ptr] = list[up_ptr];
list[up_ptr] = temp;
}
}
// Swapping list[pivot] and list[up_ptr]
temp = list[up_ptr];
list[up_ptr] = list[pivot];
list[pivot] = temp;
return up_ptr;
}
void quicksort(int list[], int low, int high)
{
int pivot;
// Array is already sorted
if(low>high)
return;
// getting the pivot index
pivot=partition(list,low,high);
// Handling the Left sub-part of pivot
quicksort(list,low,pivot-1);
// Handling the Right sub-part of pivot
quicksort(list,pivot+1,high);
}
int main()
{
int a[100],n;
// Taking the array as input and sorting using quicksort
printf("Enter the Size of Array:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the %d-th element:",i+1);
scanf("%d",&a[i]);
}
printf("Array before Sorting:\n");
print_array(a,n);
quicksort(a,0,n-1);
printf("Array After Sorting:\n");
print_array(a,n);
return 0;
}