-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
71 lines (37 loc) · 1016 Bytes
/
Copy pathQuickSort.cpp
File metadata and controls
71 lines (37 loc) · 1016 Bytes
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
#include<stdio.h>
//#include<iostream>
void swap(int* a, int* b){
int t = *a;
*a=*b;
*b= t;
}
int partition(int A[], int start, int end) {
int i = start;
int piv = A[end];
int c;
for(c=start; c<end; c++) {
if(A[c] < piv) { // Quick Sort
swap(&A[c], &A[i]);
i++;
}
}
swap(&A[i],&A[c]);
return i;
}
void quick_sort(int A[], int s, int e) {
if(s<e) {
int pi = partition(A,s,e);
quick_sort(A,s,pi-1);
quick_sort(A,pi+1,e);
}
}
int main()
{
int arr[] = {7,3,5,8,9,2,0,4};
int n = sizeof(arr)/sizeof(arr[0]);
quick_sort(arr,0,n-1);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
-------------------------------------------------------------------------------------------------------------------