-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.c
70 lines (66 loc) · 1.32 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
#include <stdio.h>
#include <stdlib.h>
int partition(int arr[], int low, int high)
{
int pivot = low;
int i = low + 1;
int j = high;
int temp;
do
{
while (arr[i] <= arr[pivot])
{
i++;
}
while (arr[j] > arr[pivot])
{
j--;
}
if (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
} while (i < j);
temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;
return j;
}
void quicksort(int arr[], int low, int high)
{
int partitionIndex;
if (low < high)
{
partitionIndex = partition(arr, low, high);
quicksort(arr, low, partitionIndex - 1);
quicksort(arr, partitionIndex + 1, high);
}
}
int main()
{
int n;
printf("Enter the length of the array:\n");
scanf("%d", &n);
printf("Enter the %d elements of the array:\n", n);
int arr[100];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("The given array is:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
quicksort(arr, 0, n - 1);
printf("The sorted array is:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}