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
|
#include <iostream>
#include <cstdlib>
// original
void quickSort(int arr[], int left, int right) {
std::cout << ">quicksort(arr,"<<left<<","<<right<<");\n";
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
std::cout << "+quicksort(i="<<i<<",j="<<j<<"\n";
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
std::cout << "<quicksort(arr,"<<left<<","<<right<<");\n";
}
int qsortCompare(const void *param1, const void *param2) {
int number1 = *(int *) param1;
int number2 = *(int *) param2;
return number1 - number2;
}
// Forward Declaration
static void myqsortpart(void *array, size_t size, int left, int right,
int (*compar)(const void *param1, const void *param2));
void myqsort(void *array, size_t nMembers, size_t size,
int (*compar)(const void *param1, const void *param2))
{
int left = 0;
int right = nMembers - 1;
myqsortpart(array, size, left, right, compar);
}
static void myqsortpart(void *array, size_t size, int left, int right,
int (*compar)(const void *param1, const void *param2))
{
std::cout << ">myqsortpart(arr,"<<left<<","<<right<<");\n";
int i = left, j = right;
int pivot = (right + left) / 2;
void *pivotptr = (void *) ((char *) array + pivot * size);
/* partition */
while (i <= j)
{
while ((*compar)((void *) ((char *) array + i * size), pivotptr) < 0)
i++;
while ((*compar)((void *) ((char *) array + j * size), pivotptr) > 0)
j--;
std::cout << "+myqsortpart(i="<<i<<",j="<<j<<"\n";
if (i <= j)
{
// swap operation
char *ptr1 = NULL, *ptr2 = NULL;
ptr1 = (char *) array + i * size;
ptr2 = (char *) array + j * size;
size_t size2;
size2 = size;
do
{
char tmp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = tmp;
ptr2++;
ptr1++;
--size2;
}
while (size2 > 0);
i++;
j--;
}
}
if (left < j)
myqsortpart(array, size, left, j, compar);
if (i < right)
myqsortpart(array, size, i, right, compar);
std::cout << ">myqsortpart(arr,"<<left<<","<<right<<");\n";
}
void printArray(int *array, int nElems)
{
for (int i = 0; i < nElems; i++)
std::cout << array[i] << ' ';
std::cout << std::endl;
}
int main(void)
{
int array[] = { 18, 20, 7, 3, 9, 14, 4, 18, 11, 5, 16, 15, 14, 18, 18, 7, 20, 10, 4, 18 };
int arraySize = sizeof(array) / sizeof(array[0]);
int *arrayMyqsort = new int[arraySize];
int *arrayQsort = new int[arraySize];
int *arrayQuicksort = new int[arraySize];
for (int i = 0; i < arraySize; i++)
{
arrayMyqsort[i] = array[i];
arrayQsort[i] = array[i];
arrayQuicksort[i] = array[i];
}
std::qsort(arrayQsort, arraySize, sizeof(array[0]), qsortCompare);
quickSort(arrayQuicksort, 0, arraySize - 1);
myqsort(arrayMyqsort, arraySize, sizeof(array[0]), qsortCompare);
std::cout << "array : " << std::endl;
printArray(array, arraySize);
std::cout << "qsort() : " << std::endl;
printArray(arrayQsort, arraySize);
std::cout << "quicksort() : " << std::endl;
printArray(arrayQuicksort, arraySize);
std::cout << "myqsort() : " << std::endl;
printArray(arrayMyqsort, arraySize);
delete[] arrayMyqsort;
delete[] arrayQsort;
delete[] arrayQuicksort;
}
|