forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort_C.c
56 lines (46 loc) · 1.14 KB
/
Quicksort_C.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
#include<stdio.h>
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int partition (int array[], int low, int high)
{
int pivot = array[high];
int swapIndex = (low - 1); //swapping index
for (int j = low; j <= high- 1; j++)
{
if (array[j] < pivot)
{
swapIndex ++;
swap(&array[swapIndex], &array[j]);
}
}
swap(&array[swapIndex + 1], &array[high]);
return (swapIndex + 1);
}
void quickSort(int array[], int low, int high)
{
if (low < high)
{
int indexPI = partition(array, low, high);
quickSort(array, low, indexPI - 1); // left partition
quickSort(array, indexPI + 1, high); // right partition
}
}
void display(int array[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", array[i]);
}
int main()
{
int array[] = {70, 90, 10, 30, 50, 20, 60};
int size = sizeof(array)/sizeof(array[0]);
quickSort(array, 0, size-1);
printf("Array after Quick Sorting: ");
display(array, size);
return 0;
}