-
Notifications
You must be signed in to change notification settings - Fork 96
/
quickSort.c
72 lines (59 loc) · 1.35 KB
/
quickSort.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
#include <stdio.h>
int partition(int arr[], int low, int high)
{
int i = low - 1, pivot = arr[high], temp;
// Partitioning
for (int j = low; j < high; j++)
{
// If current element is smaller than or
if (arr[j] < pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap pivot with i+1
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
// Main quicksort function
int quicksort(int arr[], int low, int high)
{
if (low < high)
{
// Getting pivot element
int pivot;
pivot = partition(arr, low, high);
// Recursive call for sorting left side
quicksort(arr, low, pivot - 1);
// Recursive call for sorting right side
quicksort(arr, pivot + 1, high);
}
}
int main()
{
int n;
// Taking length of array
printf("Enter array length : ");
scanf("%d", &n);
int arr[n];
// Getting array elements
printf("Enter array elements : ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Calling quicksort function
quicksort(arr, 0, n - 1);
// Printing sorted array
printf("Sorted Array is : ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}