-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksort_part.cpp
62 lines (56 loc) · 1.37 KB
/
quicksort_part.cpp
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
#include <iostream>
using namespace std;
void swap (int arr[] , int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void print (int [], int start, int end );
int quick(int arr[],int start, int end){
//given a pivot, rearrange the elements such that
//numbers<pivot || pivot || numbers > pivot
int element = arr[start];
int i = start-1,j = end+1;
while(true)
{
do {
i++;
}while(arr[i]<= element);
do{
j--;
}while (arr[j]> element);
if ( i < j)
return j;
swap (arr, i, j);
}
// returns end when the array is already sorted; thats when end will already be in place
return j;
}
void print(int arr[],int start, int end){
for (int i = start; i<= end; i++ ){
cout <<arr[i]<<" ";
}
cout <<endl;
}
int qsort ( int arr[], int start, int end ){
cout <<"\nstart: "<<start <<" end :"<<end;
if ( start < end )
{
int pivot = quick (arr, start, end );
qsort( arr, start, pivot);
qsort( arr, pivot+1, end);
}
}
bool checker (int arr[], int len )
{
for (int i =1; i< len; i++ )
if (arr[i-1 ] > arr[i])
return false;
return true;
}
int main(){
int arr[]={10,20,30,40,34,60,70,80,5};
qsort(arr, 0, 8);
print (arr, 0 , 8);
checker (arr, 9);
}