-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_sort.c
71 lines (60 loc) · 1.25 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
#include <stdio.h>
// Predefining used function
void swap(int *a, int *b);
int Part(int array[], int l, int h);
void Quick_Sort(int array[], int l, int h);
// main funtion
int main()
{
int size;
printf("\nEnter size of array : ");
scanf("%d", &size);
int array[size];
printf("Enter elements of array : \n");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
// calling Quick_sort funtion
Quick_Sort(array, 0, size - 1);
printf("\nArray after sorting using Quick sort : \n");
for (int i = 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
// swap funtion
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
// funtion for division/Part
int Part(int array[], int l, int h)
{
int k = array[h];
int i = (l - 1);
for (int j = l; j < h; j++)
{
if (array[j] <= k)
{
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[h]);
return (i + 1);
}
// Recursive Quick sort funtion
void Quick_Sort(int array[], int l, int h)
{
if (l < h)
{
int p = Part(array, l, h);
Quick_Sort(array, l, p - 1);
Quick_Sort(array, p + 1, h);
}
}