-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.c
77 lines (65 loc) · 1.34 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
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
int partition(int *arr, int length)
{
int i = 1, j = length - 1;
int temp;
while (i < j)
{
while ((arr[i] <= arr[0]) && (i < length))
{
i++;
}
while ((arr[j] > arr[0]) && (j > 0))
{
j--;
}
if (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
if (arr[j] <= arr[0])
{
temp = arr[j];
arr[j] = arr[0];
arr[0] = temp;
}
return j;
}
void quick_sort(int *arr, int length)
{
if (length > 1)
{
int partition_index;
partition_index = partition(arr, length);
quick_sort(arr, partition_index);
quick_sort((arr + partition_index + 1), (length - partition_index - 1));
}
}
int main()
{
int length = 50;
int *arr = (int *)malloc(length * sizeof(int));
srand(time(0));
for (int i = 0; i < length; i++)
{
arr[i] = rand() % 100;
printf("%d ", arr[i]);
}
printf("\n--------After Sorting------\n");
quick_sort(arr, length);
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
return 0;
}