其中的交换、幅值、比较算法可能要根据具体的排序数组类型来修改实现
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <string.h>
typedef enum
{
LESS = -1,
EQUAL = 0,
GREATER,
} Compare_Value_t;
Compare_Value_t compare_Integer(void* a, void* b)
{
return *(int*)a == *(int*)b ? EQUAL : *(int*)a > *(int*)b ? GREATER
: LESS;
}
void Swap_Integer(void* a, void* b)
{
int temp = *(int*)a;
*(int*)a = *(int*)b;
*(int*)b = temp;
}
void Copy_Interger(void* a, void* b)
{
*(int*)a = *(int*)b;
}
int partsort_hoart(void* array, size_t Elemtype_Size, int left, int right, Compare_Value_t compare(void* a, void* b), void Swap(void* a, void* b))
{
int key = left;
while (left < right)
{
while (left < right && (compare((char*)array + right * Elemtype_Size, (char*)array + key * Elemtype_Size) != LESS))
{
right--;
}
while (left < right && (compare((char*)array + left * Elemtype_Size, (char*)array + key * Elemtype_Size) != GREATER))
{
left++;
}
if (left < right)
Swap((char*)array + right * Elemtype_Size, (char*)array + left * Elemtype_Size);
}
Swap((char*)array + key * Elemtype_Size, (char*)array + left * Elemtype_Size);
return left;
}
int partsort_DigHole(void* array, size_t Elemtype_Size, int left, int right, Compare_Value_t compare(void* a, void* b), void Swap(void* a, void* b))
{
int hold = left;
void* key_value = malloc(Elemtype_Size);
memcpy(key_value, (void*)((char*)array + left * Elemtype_Size), Elemtype_Size);
while (left < right)
{
while (left < right && (compare((char*)array + right * Elemtype_Size, key_value) != LESS))
{
right--;
}
Swap((char*)array + hold * Elemtype_Size, (char*)array + right * Elemtype_Size);
hold = right;
while (left < right && (compare((char*)array + left * Elemtype_Size, key_value) != GREATER))
{
left++;
}
if (left < right)
{
Swap((char*)array + hold * Elemtype_Size, (char*)array + left * Elemtype_Size);
hold = left;
}
}
Swap((char*)array + hold * Elemtype_Size, key_value);
free(key_value);
return left;
}
int partsort_FBP(void* array, size_t Elemtype_Size, int left, int right, Compare_Value_t compare(void* a, void* b), void Swap(void* a, void* b))
{
int key = left;
int prev = left;
int cur = left + 1;
while (cur <= right)
{
if (compare((char*)array + cur * Elemtype_Size, (char*)array + key * Elemtype_Size) == LESS && ++prev != cur)
{
Swap((char*)array + prev * Elemtype_Size, (char*)array + cur * Elemtype_Size);
}
cur++;
}
Swap((char*)array + prev * Elemtype_Size, (char*)array + key * Elemtype_Size);
return prev;
}
void quicksort(void* array, size_t Elemtype_Size, int begin, int end, int partsort(void* array, size_t Elemtype_Size, int left, int right, Compare_Value_t compare(void* a, void* b), void Swap(void* a, void* b)), Compare_Value_t compare(void* a, void* b), void Swap(void* a, void* b))
{
if (begin >= end)
return;
int key = partsort(array, Elemtype_Size, begin, end, compare, Swap);
quicksort(array, Elemtype_Size, begin, key - 1, partsort, compare, Swap);
quicksort(array, Elemtype_Size, key + 1, end, partsort, compare, Swap);
}
int array[10] = {1, -1, 7, 3, 2, 20, 9, 0, 6, 10};
int array1[10] = {1, -1, 7, 3, 2, 20, 9, 0, 6, 10};
int array2[10] = {1, -1, 7, 3, 2, 20, 9, 0, 6, 10};
int main(void)
{
printf("--------------------------------------\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", array[i]);
}
printf("\n--------------------------------------\n");
quicksort((void*)array, sizeof(int), 0, sizeof(array) / sizeof(int) - 1, partsort_hoart, compare_Integer, Swap_Integer);
for (int i = 0; i < 10; i++)
{
printf("%d ", array[i]);
}
printf("\n--------------------------------------\n\n");
printf("--------------------------------------\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", array1[i]);
}
printf("\n--------------------------------------\n");
quicksort((void*)array1, sizeof(int), 0, sizeof(array1) / sizeof(int) - 1, partsort_DigHole, compare_Integer, Copy_Interger);
for (int i = 0; i < 10; i++)
{
printf("%d ", array1[i]);
}
printf("\n--------------------------------------\n");
printf("--------------------------------------\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", array2[i]);
}
printf("\n--------------------------------------\n");
quicksort((void*)array2, sizeof(int), 0, sizeof(array2) / sizeof(int) - 1, partsort_FBP, compare_Integer, Swap_Integer);
for (int i = 0; i < 10; i++)
{
printf("%d ", array2[i]);
}
printf("\n--------------------------------------\n");
return 0;
}