-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.c
59 lines (57 loc) · 1.55 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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void QuickSort(int a[], int low, int high)
{
if (low < high) // list has more than one element
{
int i, j, pivot, k, temp;
pivot = a[low]; // choose first element in the list as pivot element
i = low;
j = high;
while (i < j) // if there are elements left, to be compared with pivot element
{
while (a[i] <= pivot && i <= j)
i = i + 1;
while (a[j] > pivot)
j = j - 1;
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} // end of while
// place element at index j , at pos low
a[low] = a[j];
// place pivot element in its place, at index j
a[j] = pivot;
// call quicksort on left sub list of pivot
QuickSort(a, low, j - 1);
// call quicksort on right sub list of pivot
QuickSort(a, j + 1, high);
}
}
void main()
{
int n, *a, k;
clock_t st, et;
double ts;
printf("\n Enter How many Numbers: ");
scanf("%d", &n);
a = (int *)calloc(n, sizeof(int));
printf("\nThe array before sorting:\n");
for (k = 0; k < n; k++)
{
a[k] = n - k;
printf("%d\t", a[k]);
}
st = clock();
QuickSort(a, 0, n - 1);
et = clock();
ts = (double)(et - st) / CLOCKS_PER_SEC;
printf("\nSorted Numbers are: \n ");
for (k = 0; k < n; k++)
printf("%d\t", a[k]);
printf("\nThe time taken is %e", ts);
}