快速排序(非递归)

以下代码经测试,排序5000000(五千万)int型数据没有问题!

第一个参数是数组首地址
第二个参数是数组元素个数

typedef struct _BUFF {
    int *LeftBuff;
    int *RightBuff;
    DWORD number;//有多少个
    struct _BUFF *next;
}BUFF;
void QuickSort(int * const arr, DWORD number)//快速排序
{
    int tmp;
    int key;
    DWORD left, right;
    DWORD tmpLeft, tmpRight;
    BUFF *p = NULL;
    BUFF buff = {NULL, NULL, 0, NULL};
    buff.LeftBuff = (int *)malloc(1024*1024*sizeof (int));
    buff.RightBuff = (int *)malloc(1024*1024*sizeof (int));
    if ((NULL == buff.LeftBuff) || (NULL == buff.LeftBuff))
    {
        free(buff.LeftBuff);
        free(buff.LeftBuff);
        printf("分配缓存出错!\n");
        return;
    }
    buff.LeftBuff[buff.number] = 0;
    buff.RightBuff[buff.number] = number-1;
    buff.number++;
    p = &buff;
    while (buff.number/* && (NULL == buff.next)*/)
    {
        tmpLeft = p->LeftBuff[p->number-1];
        tmpRight = p->RightBuff[p->number-1];
        left = tmpLeft;
        right = tmpRight;
        key = arr[left++];
        do
        {
            while ((arr[left] < key) && (left < tmpRight))
            {
                left++;
            }
            while ((arr[right] >= key) && (right > tmpLeft))
            {
                right--;
            }
            if (left < right)
            {
                tmp = arr[left];
                arr[left] = arr[right];
                arr[right] = tmp;
            }
        }
        while (left < right);
        tmp = arr[right];
        arr[right] = arr[tmpLeft];
        arr[tmpLeft] = tmp;

        if (p->number >= 1024*1024-1)
        {
            p->next = (BUFF *)malloc(sizeof (BUFF));
            if (NULL != p->next)
            {
                p = p->next;
                p->LeftBuff = (int *)malloc(1024*1024*sizeof (int));
                p->RightBuff = (int *)malloc(1024*1024*sizeof (int));
                p->number = 0;
                p->next = NULL;
            }
            if ((0 != p->number) || (NULL == p->LeftBuff) || (NULL == p->RightBuff))
            {
                printf("分配缓存出错!\n");
                while (NULL != buff.next)
                {
                    p = buff.next;
                    do
                    {
                        p = p->next;
                    }
                    while (NULL != p->next);
                    free(p);
                }
                getch();
                return;
            }
        }
        else
        {
            p->number--;
        }

        if (tmpLeft+1 < right)
        {
            p->LeftBuff[p->number] = tmpLeft;
            p->RightBuff[p->number] = right-1;
            p->number++;
        }
        if (tmpRight > left)
        {
            p->LeftBuff[p->number] = left;
            p->RightBuff[p->number] = tmpRight;
            p->number++;
        }
        if ((0 == p->number) && (NULL != buff.next))
        {
            p = &buff;
            while (NULL == p->next->next)
            {
                p = p->next;
            }
            free(p->next);
            p->next = NULL;
        }
    }
}


测试代码:


#include <stdio.h>
#include <conio.h>
#include <windows.h>

typedef struct _BUFF {
    int *LeftBuff;
    int *RightBuff;
    DWORD number;//有多少个
    struct _BUFF *next;
}BUFF;

void QuickSort(int * const arr, DWORD number);//快速排序
void ExamineArr(const int * const arr, DWORD number);//检查排序结果

int main(int argc, char *argv[])
{
    DWORD StartTime, EndTime;
    DWORD i;
    DWORD num = 50000000;
    int *arr = NULL;
    arr = (int *)malloc(num * sizeof (int));
    if (NULL == arr)
    {
        free(arr);
        printf("内存分配失败,程序退出!\n");
        getch();
        return -1;
    }

    StartTime = GetTickCount();
    for (i=0; i<num; i++)
    {
        *(arr+i) = rand();
    }
    EndTime = GetTickCount();
    printf("生成%u个随机数耗时:%ums\n", num, EndTime - StartTime);
    
    StartTime = GetTickCount();
    QuickSort(arr, num);
    EndTime = GetTickCount();
    printf("快速排序耗时:%ums\n", EndTime - StartTime);
    ExamineArr(arr, num);//检查排序结果

    free(arr);
    getch();
    return 0;
}

void QuickSort(int * const arr, DWORD number)//快速排序
{
    int tmp;
    int key;
    DWORD left, right;
    DWORD tmpLeft, tmpRight;
    BUFF *p = NULL;
    BUFF buff = {NULL, NULL, 0, NULL};
    buff.LeftBuff = (int *)malloc(1024*1024*sizeof (int));
    buff.RightBuff = (int *)malloc(1024*1024*sizeof (int));
    if ((NULL == buff.LeftBuff) || (NULL == buff.LeftBuff))
    {
        free(buff.LeftBuff);
        free(buff.LeftBuff);
        printf("分配缓存出错!\n");
        return;
    }
    buff.LeftBuff[buff.number] = 0;
    buff.RightBuff[buff.number] = number-1;
    buff.number++;
    p = &buff;
    while (buff.number/* && (NULL == buff.next)*/)
    {
        tmpLeft = p->LeftBuff[p->number-1];
        tmpRight = p->RightBuff[p->number-1];
        left = tmpLeft;
        right = tmpRight;
        key = arr[left++];
        do
        {
            while ((arr[left] < key) && (left < tmpRight))
            {
                left++;
            }
            while ((arr[right] >= key) && (right > tmpLeft))
            {
                right--;
            }
            if (left < right)
            {
                tmp = arr[left];
                arr[left] = arr[right];
                arr[right] = tmp;
            }
        }
        while (left < right);
        tmp = arr[right];
        arr[right] = arr[tmpLeft];
        arr[tmpLeft] = tmp;

        if (p->number >= 1024*1024-1)
        {
            p->next = (BUFF *)malloc(sizeof (BUFF));
            if (NULL != p->next)
            {
                p = p->next;
                p->LeftBuff = (int *)malloc(1024*1024*sizeof (int));
                p->RightBuff = (int *)malloc(1024*1024*sizeof (int));
                p->number = 0;
                p->next = NULL;
            }
            if ((0 != p->number) || (NULL == p->LeftBuff) || (NULL == p->RightBuff))
            {
                printf("分配缓存出错!\n");
                while (NULL != buff.next)
                {
                    p = buff.next;
                    do
                    {
                        p = p->next;
                    }
                    while (NULL != p->next);
                    free(p);
                }
                getch();
                return;
            }
        }
        else
        {
            p->number--;
        }

        if (tmpLeft+1 < right)
        {
            p->LeftBuff[p->number] = tmpLeft;
            p->RightBuff[p->number] = right-1;
            p->number++;
        }
        if (tmpRight > left)
        {
            p->LeftBuff[p->number] = left;
            p->RightBuff[p->number] = tmpRight;
            p->number++;
        }
        if ((0 == p->number) && (NULL != buff.next))
        {
            p = &buff;
            while (NULL == p->next->next)
            {
                p = p->next;
            }
            free(p->next);
            p->next = NULL;
        }
    }
}

void ExamineArr(const int * const arr, DWORD number)
{
    DWORD i=0, j=1;
    if (number <2)
    {
        return;
    }
    for (; j<number; i++,j++)
    {
        if (arr[i] > arr[j])
        {
            printf("第%u个数大于第%u个数!\n", i, j);
            return;
        }
    }
}


你可以使用迭代实现快速排序非递归版本。下面是一个示例代码: ```cpp #include <iostream> #include <stack> using namespace std; // 交换两个元素的值 void swap(int& a, int& b) { int temp = a; a = b; b = temp; } // 快速排序非递归实现 void quickSort(int arr[], int low, int high) { stack<int> stk; stk.push(low); stk.push(high); while (!stk.empty()) { high = stk.top(); stk.pop(); low = stk.top(); stk.pop(); int pivot = arr[high]; int i = low - 1; for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); if (i + 1 < high) { stk.push(i + 2); stk.push(high); } if (i + 1 > low) { stk.push(low); stk.push(i); } } } // 打印数组 void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int arr[] = {5, 2, 8, 12, 3, 7}; int size = sizeof(arr) / sizeof(arr[0]); cout << "原始数组: "; printArray(arr, size); quickSort(arr, 0, size - 1); cout << "排序后数组: "; printArray(arr, size); return 0; } ``` 这段代码使用了一个栈来模拟递归调用的过程。在每一次循环中,取出栈中保存的low和high值,然后进行快速排序的划分操作。如果划分后的左半部分和右半部分的长度大于1,就将新的low和high值压入栈中,以便后续继续排序。通过循环不断处理栈中的任务,直到栈为空,即完成了整个排序过程。 注意,这段代码只是一个示例,实际使用时可能需要根据具体情况进行适当的修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值