-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
executable file
·165 lines (105 loc) · 3.08 KB
/
QuickSort.cpp
File metadata and controls
executable file
·165 lines (105 loc) · 3.08 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef QUICKSORT_CPP_
#define QUICKSORT_CPP_
#include "QuickSort.h"
template <class T>
QuickSort<T>::QuickSort(T ** p_teraSortItem)
{
teraSortItem = p_teraSortItem;
}
template <class T>
void QuickSort<T>::quickSort(int64_t low, int64_t high)
{
int64_t i = low;
int64_t j = high;
int64_t pi = (i+j)/2;
// T* pivot = teraSortItem[pi];
uint128_t pivot = teraSortItem[pi]->key();
// cout<<"low: "<<i<<" high: "<<j <<" pivot: "<<pi<<endl;
while (i <= j)
{
while (teraSortItem[i]->key() < pivot)
i++;
while (teraSortItem[j]->key() > pivot)
j--;
if (i <= j)
{
teraSortItem[i]->swap(teraSortItem[j]);
i++;
j--;
}
}
if (j > low)
{ quickSort(low, j); }
if (i < high)
{quickSort(i, high); }
}
/*
// ================================geeks for geeks=============================
template <class T>
int64_t QuickSort<T>:: partition(int64_t l , int64_t r ){
T* pivot = teraSortItem [r];
//uint128_t pivot = teraSortItem[r]->getKeyValue();
int64_t i = (l-1);
for(int64_t j=l; j<= r-1; j++){
if(teraSortItem[j] <= pivot){
i++;
// std::swap(teraSortItem[i],teraSortItem[j]);
teraSortItem[i]->swap(teraSortItem[j]);
}
}
// std::swap(teraSortItem[i+1],teraSortItem[r]);
teraSortItem[i+1]->swap(teraSortItem[r]);
return (i+1);
}
template <class T>
void QuickSort<T>::quickSort( int64_t l, int64_t r){
if(l < r){
int64_t pi = partition(l,r);
cout<<"left: "<<l<<" pi: "<<pi<<" right: "<<r<<endl;
quickSort(l, pi-1);
quickSort(pi+1, r);
}
}
*/
/*
------------------code aldoctor ------------------------
template <class T>
T* QuickSort<T>:: partition(int64_t left , int64_t right )
{
T** m = NULL;
int64_t center=(left+right)/2;
if(teraSortItem[center]<teraSortItem[left])
teraSortItem[center]->swap(teraSortItem[left]);
if ( teraSortItem[right] < teraSortItem[left] )
teraSortItem[right]->swap(teraSortItem[left]);
if ( teraSortItem[right] < teraSortItem[center] )
teraSortItem[right]->swap(teraSortItem[left]);
teraSortItem[center]->swap(teraSortItem[right-1]);
return teraSortItem[right-1];
}
template <class T>
void QuickSort<T>::quickSort (int64_t left, int64_t right)
{
T * pivot = partition(left,right);
int64_t i = left, j = right-1;
for (;;)
{
while (teraSortItem[++i] < pivot);
while ( pivot < teraSortItem[--j]);
if ( i < j ) teraSortItem[i]->swap(teraSortItem[j]); else break;
}
teraSortItem[i]->swap(teraSortItem[right-1]);
quickSort(left,i-1);
quickSort(i+1,right);
}
/*
template <typename T>
void quickSort (uint64_t list_size)
{
QuickSort<T>::quickSort (0,list_size-1);
}
*/
template <class T>
QuickSort<T>::~QuickSort()
{}
#endif