-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.c
More file actions
88 lines (85 loc) · 1.72 KB
/
quickSort.c
File metadata and controls
88 lines (85 loc) · 1.72 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000
int count = 0;
int partition(int a[SIZE], int l, int r)
{
int pivot = a[l];
int i = l + 1, j = r, temp;
while (1)
{
while (pivot >= a[i] && i <= r)
{
i++;
count++;
}
while (pivot < a[j] && j != l)
{
j--;
count++;
}
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
temp = a[j];
a[j] = a[l];
a[l] = temp;
return j;
}
}
}
void quickSort(int a[SIZE], int l, int r)
{
if (l < r)
{
int s = partition(a, l, r);
quickSort(a, l, s - 1);
quickSort(a, s + 1, r);
}
}
int main()
{
int a[SIZE], x[SIZE], y[SIZE], z[SIZE];
int n, i, j, c1, c2, c3;
printf("\nEnter the size of the array :");
scanf("%d", &n);
printf("\nRead the elements: ");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
quickSort(a, 0, n - 1);
printf("\nSorted array : ");
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
printf("\nCount = %d\n", count);
printf("\nSize\tASC\tDESC\tRAND\n");
for (i = 16; i < SIZE; i *= 2)
{
for (j = 0; j < i; j++)
{
x[j] = j;
y[j] = i - j - 1;
z[j] = rand() % i;
}
count = 0;
quickSort(x, 0, i - 1);
c1 = count;
count = 0;
quickSort(y, 0, i - 1);
c2 = count;
count = 0;
quickSort(z, 0, i - 1);
c3 = count;
printf("%d\t%d\t%d\t%d\n", i, c1, c2, c3);
}
return 0;
}