-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquick_sort_prac1.c
65 lines (45 loc) · 1.02 KB
/
quick_sort_prac1.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
#include <stdio.h>
#define MAX 1000
int num;
int arr[MAX];
int partition(int arr[], int start, int end)
{
int pIndex;
int k = start;
int pivot = end;
pIndex = start;
while(k < end) {
if (arr[k] <= arr[pivot]) {
int temp = arr[pIndex];
arr[pIndex] = arr[k];
arr[k] = temp;
pIndex++;
}
k++;
}
int temp = arr[pIndex];
arr[pIndex] = arr[pivot];
arr[pivot] = temp;
return pIndex;
}
void QuickSort(int arr[], int start, int end)
{
if (start < end) {
int pIndex = partition(arr, start, end);
QuickSort(arr, start, pIndex-1);
QuickSort(arr, pIndex+1, end);
}
}
int main()
{
int k;
printf("Welcome to Quick Sort algorithms...\n");
printf("Enter number of elements in the list...\n");
scanf("%d", &num);
for(k=0; k < num; k++)
scanf("%d", &arr[k]);
QuickSort(arr, 0, num-1);
printf("\n");
for(k=0; k < num; k++)
printf("[%d]", arr[k]);
}