通用数组快速排序C语言 3种方法

其中的交换、幅值、比较算法可能要根据具体的排序数组类型来修改实现

#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;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值