-
Notifications
You must be signed in to change notification settings - Fork 14
/
Quick_Sort
53 lines (48 loc) · 1022 Bytes
/
Quick_Sort
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
#include <stdio.h>
int partition(int ar[], int low, int high)
{
int pivot = ar[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (ar[j] <= pivot)
{
i++;
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
int temp = ar[i + 1];
ar[i + 1] = ar[high];
ar[high] = temp;
return i + 1;
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main(void)
{
puts("Enter the number of elements in the array: ");
int n;
scanf("%d", &n);
int arr[n];
puts("Enter the elements of the array: ");
for (int i = 0; i < n; i++)
{
printf("%d: ", i);
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
puts("The sorted array is: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}