-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
57 lines (49 loc) · 1.04 KB
/
QuickSort.cpp
File metadata and controls
57 lines (49 loc) · 1.04 KB
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
#include <stdio.h>
int partition(int A[], int low, int high)
{
int key = A[low];
int i = low;
int j = high;
int temp;
while (i < j){
while (A[i] <= key){
i++;
}
while (A[j] > key){
j--;
}
if (i < j){
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[low];
A[low] = A[j];
A[j] = temp;
return j;
}
void quickSort(int A[], int low, int high){
int j;
if (low < high){
j = partition(A, low, high);
quickSort(A, low, j - 1);
quickSort(A, j + 1, high);
}
}
int main(){
int n;
printf("Enter the length of array : ");
scanf("%d",&n);
int Arr[n];
printf("Enter the elements of the array : ");
for(int i=0;i<n;i++){
scanf("%d",&Arr[i]);
}
printf("\n---below is sorted array---\n");
quickSort(Arr, 0, n - 1);
for(int i=0;i<n;i++){
printf("%d ",Arr[i]);
}
return 0;
}