博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表的快速排序算法
阅读量:4501 次
发布时间:2019-06-08

本文共 1349 字,大约阅读时间需要 4 分钟。

#include
#include
using namespace std;//定义结构体typedef struct Node{ int val; Node* next;} Node;void swap(Node* a,Node* b){ int tmp = a->val; a->val = b->val; b->val = tmp;}//找到分割点Node* getSeparator(Node* pBegin,Node* pEnd){ Node* p = pBegin; Node* q = pBegin->next; int key = p->val; while(q!=pEnd){ if(q->val < key){ p = p->next; swap(p,q); } q = q->next; } swap(pBegin,p); return p;}//快速排序void fastSort(Node* pBegin,Node* pEnd){ if(pBegin != pEnd){ Node* separator = getSeparator(pBegin,pEnd); fastSort(pBegin,separator); fastSort(separator->next,pEnd); }}//创建链表Node* createNode(){ int in; Node* n; cin >> in; if(in == 1000){ n = NULL; }else{ n = new Node(); n->val = in; n->next = createNode(); } return n;}int main(){ Node* n1 = createNode(); cout << "++++++++++++++排序前+++++++++++++++++"<< endl; Node* cur = n1; while(cur!=NULL){ cout<
val<
next; } fastSort(n1,NULL); cout << "++++++++++++++排序后+++++++++++++++++"<< endl; cur = n1; while(cur!=NULL){ cout<
val<
next; } return 0;}

 

转载于:https://www.cnblogs.com/ningvsban/p/4061844.html

你可能感兴趣的文章
UIView下使用Animation控制动画
查看>>
TP之空操作及View模块
查看>>
shiro学习笔记:授权管理
查看>>
Java 使用正则表达式取出图片地址以及跳转的链接地址,来判断死链(一)
查看>>
代理delegate、NSNotification、KVO在开发中的抉择
查看>>
剑指offer--二叉搜索树的后序遍历序列
查看>>
Selenium学习第一章:搭建测试环境
查看>>
SASS笔记
查看>>
2.学习Application
查看>>
php第二十五节课
查看>>
CS224d lecture 6札记
查看>>
[NOIP 2011]聪明的质监员
查看>>
[Sdoi2013]spring
查看>>
TopCoder SRM 633 Div.2 500 Jumping
查看>>
iOS 相关博客清单
查看>>
GLSL新手上路 -- 《交互式计算机图形学》附录中GLSL代码有误 -- 修改如下
查看>>
xss挑战赛小记 0x02(prompt(1))
查看>>
软件工程 第四课(毕业论文管理系统——面向对象)
查看>>
springboot 获取post请求参数
查看>>
Netty4/Android6 SSL双向认证 攻略 2016.10.13
查看>>