-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuickSort2.c
54 lines (50 loc) · 1.08 KB
/
QuickSort2.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
#include <stdio.h>
// Traversal
void display(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int partion(int arr[],int low,int high){
int pivet = arr[low];
int start=low,end=high,temp;
while (start<end)
{
while (arr[start]<=pivet)
{
start++;
}
while (arr[end]>pivet)
{
end--;
}
if (start<end)
{
temp=arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
temp = arr[low];
arr[low] = arr[end];
arr[end] = temp;
return end;
}
void QuickSort(int arr[],int low,int high){
int partitionIndex;
if (low<high)
{
partitionIndex=partion(arr,low,high);
QuickSort(arr,low,partitionIndex-1);
QuickSort(arr,partitionIndex+1,high);
}
}
int main()
{
int arr[] = {2,5,23,4,1,45,79,6};
QuickSort(arr,0,7);
display(arr,8);
return 0;
}