单链表的快排

本文介绍了一种使用链表实现的快速排序算法,并提供了完整的C++代码实现。该算法通过链表节点的移动来完成排序过程,避免了传统数组排序中的元素复制操作,提高了排序效率。
/*
    Author:cavehubiao
    Mail:cavehubiao@qq.com
    MyBlog:http//www.cnblogs.com/cavehubiao
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
#include<queue>
#include<ctime>
using namespace std;

struct st
{
    st* next;
    int value;
    st(int va,st* p=NULL):value(va),next(p){
    }
};

void lqsort(st*left,st*right)
{
    if(left!=right)
    {
        st* slow=left;
        st* fast=left->next;
        
        for(;fast!=right;fast=fast->next)
        {
            if(fast->value<=left->value)
            {
                slow=slow->next;
                swap(slow->value,fast->value);
            }
        }
        //slow=slow->next;
        swap(slow->value,left->value);
        
        lqsort(left,slow);
        lqsort(slow->next,right);
    }
}
int main()
{
    srand((unsigned)time(NULL));
    int len;
    cin>>len;
    
    st* List=NULL,*pre=NULL;
    for(int i=0;i<len;i++)
    {
        st* pnode=new st(rand()%1000);
        if(pre==NULL)
        {
            List=pnode;
            pre=pnode;
        }
        else
        {
            pre->next=pnode;
            pre=pnode;
        }
        
    }
    lqsort(List,NULL);
    
    for(int i=0;i<len;i++)
    {
        cout<<List->value<<' ';
        st* tmp=List;
        List=List->next;
        delete tmp;
    }

  return 0;
}

 

转载于:https://www.cnblogs.com/cavehubiao/p/3679091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值