-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.h
206 lines (183 loc) · 7.14 KB
/
QuickSort.h
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef QUICKSORT_H
#define QUICKSORT_H
/*
Name: QuickSort
Author: Matthew AhSam
Date: 12/17/2013 04:04
Description: QuickSort.h
Requirements: Comparison function - int (*compare) (T,T) - T1 < T2 return -1 T1 == T2 return 0 T1 > T2 return 1;
Overloaded = operator - Makes use of = operator as a copy
Example compare function
int numberCompare (int i1, int i2) {
if (i1 < i2) {
return -1;
} else {
if (i1 == i2) {
return 0;
} else {
return 1;
}
}
}
*/
#include <stdlib.h>
template <class T>
class QuickSort {
public:
//Varibale
T * fullArray;
int fullArraySize;
int (*compare) (T,T); //T1 <= T2 return true, T1 > T2 return false
//Functions
QuickSort(int,T *,int (*)(T,T)); // int (*compare) (T,T) function pointer should be in callback function for comparisons
~QuickSort();
T * Sort();
void DisplayArray (int,T *);
private:
T * SortPriv (int, T *);
};
template <class T>
void QuickSort <T>::DisplayArray (int arraySize, T * array) {
std::cout << std::endl << "Array: {";
std::cout << array [0];
for (int i = 1; i < arraySize; i++) {
std::cout << ", " << array [i] ;
}
std::cout << "}" << std::endl;
}
template <class T>
T * QuickSort <T>::Sort () {
return SortPriv(fullArraySize, fullArray);
}
template <class T>
T * QuickSort <T>::SortPriv (int arraySize, T * sortArray){
T * leftSelector = &sortArray [0];
int leftCounter = 0;
T * rightSelector = &sortArray [arraySize-1];
int rightCounter = arraySize-1;
int seed = rand()%arraySize;
T pivot = sortArray[seed];//Random pivot
T temp;
bool hasMoved;
int movedCounter = 0;
std::cout << std::endl << "START SORT FUNCTION!" << std::endl << "Pivot " << pivot << std::endl << "Array Size: " << arraySize << std::endl;
while (leftSelector != rightSelector) {
hasMoved = false;
DisplayArray(fullArraySize,fullArray);
std::cout << "Left " << *leftSelector << " ";
while (1 == (compare (pivot,*leftSelector))) { //Keep getting next element while pivot is greater than left selector
if (leftSelector == rightSelector){
break;
}
leftCounter++;
leftSelector = &sortArray [leftCounter];
std::cout << "Picking LEFT: " << *leftSelector << " ";
hasMoved = true;
}
std::cout << std::endl << "Right " << *rightSelector << " ";
while (-1 == (compare (pivot,*rightSelector))) { //Keep getting next element while pivot is less than right selector
if (leftSelector == rightSelector){
break;
}
rightCounter--;
rightSelector = &sortArray [rightCounter];
std::cout << "Picking RIGHT: " << *rightSelector << " ";
hasMoved = true;
}
temp = *leftSelector;
*leftSelector = *rightSelector;
*rightSelector = temp;
if (hasMoved == false && *leftSelector == *rightSelector) { //If there are multiples of the chosen pivot this if statement
if(leftSelector+1 == rightSelector){
break; //Array is sorted if the pivots are adjacent
}
if (leftSelector == &sortArray[0 + movedCounter]) { //If leftselector is farthest left move to next element
leftSelector++;
} else {
//Switch leftSelector with far left position
temp = sortArray[0 + movedCounter];
sortArray[0 + movedCounter] = *leftSelector;
*leftSelector = temp;
}
if (rightSelector == &sortArray [arraySize - 1 - movedCounter]) { //If rightselector is farthest right move to next element
rightSelector--;
} else {
//Switch rightSelector with far right position
temp = sortArray [arraySize - 1 - movedCounter];
sortArray [arraySize-1-movedCounter] = *rightSelector;
*rightSelector = temp;
}
movedCounter++;
}
DisplayArray(fullArraySize,fullArray);
//system("PAUSE");
}
//Put pivots back in middle
T endPoint = *leftSelector;
for (int i = 0 ;i < movedCounter; i++) {
if (endPoint < pivot){
T * tempPointer = leftSelector - i;
temp = *tempPointer;
*tempPointer = sortArray [0 + i];
sortArray [0 + i] = temp;
tempPointer = leftSelector + i + 1;
temp = *tempPointer;
*tempPointer = sortArray [arraySize - 1 - i];
sortArray [arraySize - 1 - i] = temp;
} else {
if (endPoint == pivot) {
T * tempPointer = leftSelector - i - 1;
temp = *tempPointer;
*tempPointer = sortArray [0 + i];
sortArray [0 + i] = temp;
tempPointer = leftSelector + i + 1;
temp = *tempPointer;
*tempPointer = sortArray [arraySize - 1 - i];
sortArray [arraySize - 1 - i] = temp;
} else { // *leftSelector > pivot
T * tempPointer = leftSelector - i - 1;
temp = *tempPointer;
*tempPointer = sortArray [0 + i];
sortArray [0 + i] = temp;
tempPointer = leftSelector + i;
temp = *tempPointer;
*tempPointer = sortArray [arraySize - 1 - i];
sortArray [arraySize - 1 - i] = temp;
}
}
}
DisplayArray(fullArraySize,fullArray);
//system("PAUSE");
if (arraySize > 2) {
T * sizePointer = &sortArray [0];
int countSize = 0;
while (compare(pivot,*sizePointer) != 0) {
sizePointer++;
countSize++;
}
if (countSize >0) {
SortPriv(countSize,sortArray);
}
sizePointer = &sortArray [arraySize - 1];
countSize = 0;
while(compare(pivot,*sizePointer) != 0) {
sizePointer--;
countSize++;
}
sizePointer++;
if (countSize >0) {
SortPriv(countSize,sizePointer);
}
}
return sortArray;
}
template <class T>
QuickSort <T>::QuickSort(int arraySize, T * arrayPointer, int (*typeCompare) (T,T)) {
fullArraySize = arraySize;
fullArray = arrayPointer;
compare = typeCompare;
}
template <class T>
QuickSort <T>::~QuickSort () {
}
#endif