-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuick_sort.c
59 lines (54 loc) · 1.1 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
#include <stdio.h>
// Traversal
void display(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int partition(int arr[],int low,int high){
int pivet = arr[low];
int i=low;
int j=high;
int temp;
while (i<j)
{
while (arr[i]<=pivet)
{
i++;
}
while (arr[j]>pivet)
{
j--;
}
if (i<j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
return j;
}
void QuickSort(int arr[],int low,int high){
int partitionindex;
if (low<high)
{
partitionindex= partition(arr,low,high);
QuickSort(arr,low,partitionindex-1);
QuickSort(arr,partitionindex+1,high);
}
}
int main()
{
int arr[] = {3,5,2,1,4,78,9};
display(arr,7);
printf("\n");
QuickSort(arr,0,7);
display(arr,7);
return 0;
}