-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
85 lines (76 loc) · 2.2 KB
/
quicksort.cpp
File metadata and controls
85 lines (76 loc) · 2.2 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
//https://ide.codingblocks.com/s/33198
//https://stackoverflow.com/questions/6740183/quicksort-with-first-element-as-pivot-example
int partition(int input[],int start,int end){
// int counter=0;
// for(int i=0;i<=end;i++)
// { if(input[i]<input[0])
// counter++;
// }
// swap(input[0], input[counter+1]);
// //temp = input[counter+1];
// //input[counter+1]=input[0];
// // input[0]= temp;
// int i=0;
// int j=end-1;
// while(j!=counter+1 || i!=counter+1){
// if(input[i]>input[counter+1] && input[j]<input[counter+1])
// swap(input[i], input[j]);
// i++;
// j--;
// if(input[i]>input[counter+1] && input[j]>input[counter+1])
// j--;
// if(input[i]<input[counter+1] && input[j]<input[counter+1])
// i++;
// }
// return counter+1;
int pivot=input[start];
int p1=start+1;
int j;
for(int i=start+1;i<=end;i++)
{if(input[i]<pivot)
{
if(i!=p1){ //checking for same elements no swapping required
swap(input[p1],input[i]);
}
p1++;
}
}
//moving pivot to its location
input[start]=input[p1-1];
input[p1-1]=pivot;
return p1-1;
}
void quicksort(int input[],int starting,int ending){
if(starting < ending)
{int c = partition(input,starting,ending);
quicksort(input,starting,c-1);
quicksort(input,c+1,ending);
}
}
void quickSort(int input[], int size) {
/* Don't write main().
Don't read input, it is passed as function argument.
Change in the given array itself.
Taking input and printing output is handled automatically.
*/quicksort(input,0,size-1);
// int starting =0;
// int end = size -1;
// int c = partition(input,starting,end);
//quicksort(input,starting,c-1);
//quicksort(input,c+1,end);
}
int main(){
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++) {
cin >> input[i];
}
quickSort(input, n);
for(int i = 0; i < n; i++) {
cout << input[i] << " ";
}
delete [] input;
}