-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailRecursiveQuicksort.cpp
More file actions
47 lines (41 loc) · 1022 Bytes
/
Copy pathtailRecursiveQuicksort.cpp
File metadata and controls
47 lines (41 loc) · 1022 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
#include<iostream>
using namespace std;
int Partition(int arr[], int start, int end)
{
int pivot = arr[end];
int pIndex = start;
for (int i = start; i < end; i++)
{
if (arr[i] <= pivot)
{
swap(arr[i], arr[pIndex]);
pIndex++;
}
}
swap (arr[pIndex], arr[end]);
return pIndex;
}
void tailRecursiveQuicksort(int arr[],int start,int end){
while(start < end){
int pivot = Partition(arr,start,end);
//recur on smaller arry
if(pivot - start < end - pivot ){
tailRecursiveQuicksort(arr,start,pivot - 1);
start = pivot + 1;
}
else{
tailRecursiveQuicksort(arr,pivot + 1,end);
end = pivot - 1;
}
}
}
int main()
{
int arr[] = { 3, 8, 5, 4, 1, 9, -2 };
int size = (*(&arr + 1) - arr);
tailRecursiveQuicksort(arr, 0,size - 1);
for (int k = 0; k < (*(&arr + 1) - arr); ++k) {
cout<<arr[k]<<" ";
}cout<<"\n";
return 0;
}