Open main menu

CDOT Wiki β

Changes

The Bean Counters

1,268 bytes added, 15:55, 2 April 2018
no edit summary
===== insertion sort =====
template<typename T>
inline void insertionSort(T * array, int size) {
T curr;
int j;
for (int i = 0; i < size; i++) {
curr = array[i];
for (j = i; j > 0 && array[j - 1] > curr; j--) {
array[j] = array[j - 1];
}
array[j] = curr;
}
}
===== merge sort =====
template<typename T>
inline void mergeSort(T * array, int size) {
T * temp = new T[size];
mergeSort(array, temp, 0, size - 1);
delete[] temp;
}
template<typename T> inline void mergeSort(T * array, T * temp, int start, int end) { if (start < end) { int mid ===== (start + end) / 2; mergeSort(array, temp, start, mid); mergeSort(array, temp, mid + 1, end); merge sort =====(array, temp, start, mid + 1, end); } }
template<typename T>
inline void merge(T * array, T * temp, int startA, int startB, int endB) {
int aptr = startA;
int bptr = startB;
int idx = startA;
while (aptr < startB && bptr < endB + 1) {
if (array[aptr] < array[bptr]) {
temp[idx] = array[aptr];
idx++;
aptr++;
}
else {
temp[idx++] = array[bptr];
bptr++;
}
}
while (aptr<startB) {
temp[idx] = array[aptr];
idx++;
aptr++;
}
while (bptr < endB + 1) {
temp[idx++] = array[bptr];
bptr++;
}
for (int i = startA; i <= endB; i++) {
array[i] = temp[i];
}
}
===== heap sort =====