快速排序(c)

快速排序是通过一趟排序,确定某一个元素的位置,然后将分为左右两部分,左边都比这个元素小,而右边都比这个元素大,然后左右两边继续快速排序。

快速排序是典型的的分治算法

#include"../init.h"


//一趟排序
int part(RedType *node, int low, int high){
    KeyType point = node[low].key;
    while(low < high){
        while(low < high && node[high].key >= point){
            --high;
        }
        exchange( &node[low], &node[high]);
        while(low < high && node[low].key <= point){
            ++low;
        }
        exchange( &node[low], &node[high]);
    }
    return low;
}

//一趟排序的改进版本
//交换过程中对枢纽记录的赋值是多余的
int new_part(RedType *node, int low, int high){
    RedType p = node[low];
    KeyType point = node[low].key;
    while(low < high){
        while(low < high && node[high].key >= point){
            --high;
        }
        node[low] = node[high];
        while(low < high && node[low].key <= point){
            ++low;
        }
        node[high] = node[low];
    }
    node[low] = p;
    return low;
}

//快速排序
//一趟排序后,拆分为两个子部分
void QuickSort(RedType *node, int low, int high){

    if(low < high){
        int mid = new_part(node,low,high);
        QuickSort(node,low,mid-1);
        QuickSort(node,mid+1,high);
    }
}


int main(){
    Sqlist L;
    init(&L);
    QuickSort(L.r,1,L.length);
    show(L.r);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值