-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24_quick_sort.c
76 lines (62 loc) · 1.37 KB
/
24_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
#include<stdio.h>
// Function to Sort Array using Quick Sort Algorithm
void quick_sort(int arr[], int low, int high)
{
int pi;
if(low < high)
{
pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}
// Function to Partition
int partition(int arr[], int low, int high)
{
int pi = arr[low];
int left = low + 1;
int right = high;
int temp;
do
{
while(arr[left] <= pi)
left++;
while(arr[right] > pi)
right--;
if(left < right)
{
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}while(left < right);
temp = arr[low];
arr[low] = arr[right];
arr[right] = temp;
return right;
}
int main()
{
int arr[10];
int n, index;
printf("Enter Number of Elements : ");
scanf("%d", &n);
if(n > 10 || n < 0)
{
printf("Invalid Input");
return 0;
}
printf("Enter Elements\n");
for(index = 0 ; index < n ; index++)
{
printf("Element %d : ", index + 1);
scanf("%d", &arr[index]);
}
quick_sort(arr, 0, n - 1);
printf("\nSorted Array\n");
for(index = 0 ; index < n ; index++)
{
printf("Element %d : %d\n", index + 1, arr[index]);
}
return 0;
}