-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksort_b.c
56 lines (43 loc) · 962 Bytes
/
quicksort_b.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
#include <stdio.h>
int num;
int arr[100];
int partition(int arr[], int start, int end)
{
int pivot = end;
int pIndex = start;
int k;
int temp;
for(k=start; k < end; k++) {
if (arr[k] <= arr[pivot]) {
int temp = arr[pIndex];
arr[pIndex] = arr[k];
arr[k] = temp;
pIndex++;
}
}
temp = arr[pivot];
arr[pivot] = arr[pIndex];
arr[pIndex] = 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...\n");
printf("Enter no of nodes..\n");
scanf("%d", &num);
for(k=0; k < num; k++) {
scanf("%d", &arr[k]);
}
QuickSort(arr, 0, num-1);
for(k=0; k < num; k++)
printf("[%d]", arr[k]);
}