-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick Sort.c
78 lines (60 loc) · 1.23 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
#include<stdio.h>
void dispArr(int arr_0[], int n)
{
int i;
printf("The Array: ");
for(i=0; i<n; i++)
{
printf("%d", arr_0[i]);
(i != n-1) ? printf(" ") : printf("\n");
}
}
//last element as pivot
int qPartion_l(int arr_0[], int l, int r)
{
int p = arr_0[r];
int i=0, j=0, temp;
for(j=0; j <= r-1; j++)
{
if(arr_0[j] <= p)
{
temp = arr_0[j];
arr_0[j] = arr_0[i];
arr_0[i] = temp;
i++;
}
}
//now set pivot to it's correct position
temp = arr_0[i];
arr_0[i] = arr_0[r]; // pivot placed on correct position
arr_0[r] = temp;
return i;
}
void quickSort(int arr_0[], int l, int r)
{
if(l < r)
{
int p = qPartion_l(arr_0, l, r);
quickSort(arr_0, l, p-1);
quickSort(arr_0, p+1, r);
}
}
int main()
{
int n, i, j;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr_0[n];
printf("Enter array data: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr_0[i]);
}
printf("\n");
dispArr(arr_0, n);
printf("\n");
quickSort(arr_0, 0, n-1);
printf("After Quick Sorting: ");
dispArr(arr_0, n);
return 0;
}