-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquicks.cpp
More file actions
75 lines (60 loc) · 1011 Bytes
/
quicks.cpp
File metadata and controls
75 lines (60 loc) · 1011 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
72
73
74
/*
*Name :Sharad Dixit
*Code : Quick sort
*/
#include <iostream>
using namespace std;
void show ( int a[] , int num );
void quick_sort ( int a[] , int left ,int right)
{
int pivot;
int start;
int end;
int mid = ( left+ right )/2;
pivot = a[mid];
start = left;
end = right;
while ( start < end ) {
while ( a [ start ] < pivot ) {
start++;
}
while ( a [ end ] > pivot ) {
end --;
}
if ( start <= end ) {
swap( a [start],a[end]);
end--;
start++;
}
}
if ( left < end ){
quick_sort(a,left,end);
}
if ( start < right){
quick_sort( a, start,right);
}
}
void show ( int a[] , int num ){
for ( int i = 0; i < num ; i++ ){
cout << a [i] << " ";
}
cout << endl;
}
void swap( int *a , int *b )
{
int temp = *a;
*a = *b;
*b = temp;
}
int main ()
{
int a [100];
int num;
cin >> num;
for ( int i = 0; i < num ; i++){
cin >> a[i];
}
show ( a , num );
quick_sort( a , 0 , num-1 );
show ( a , num );
}