forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.c
52 lines (49 loc) · 839 Bytes
/
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
enum {MAX_SIZE = 1000};
int a[MAX_SIZE];
void fillArray(int a[], int size)
{
int i;
for (i = 0; i < size; i++)
a[i] = rand();
}
void swap(int a[], int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int partition(int a[], int left, int right)
{
int first = left-1;
int last = right;
for (;;)
{
while (a[++first] < a[right]);
while (a[right] < a[--last])
if (last == left)
break;
if (first >= last)
break;
swap(a, first, last);
}
swap(a, first, right);
return first;
}
void quicksort(int a[], int left, int right)
{
if (right > left)
{
int mid = partition(a, left, right);
quicksort(a, left, mid - 1);
quicksort(a, mid + 1, right);
}
}
int main(void)
{
fillArray(a, MAX_SIZE);
quicksort(a, 0, MAX_SIZE - 1);
return 0;
}