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

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

#include 
#include
#include
#include
#include
using namespace std;struct LinkNode{
int val; LinkNode *next; LinkNode(int x): val(x) {
next = NULL; }};LinkNode *partition_iter(LinkNode *head, LinkNode *tail){
int pivot = head->val; LinkNode *j = head; LinkNode *i = head->next; for (; i != tail; i = i->next) {
if(i->val < pivot) {
j = j->next; swap(i->val, j->val); } } swap(head->val, j->val); return j;}void qSort(LinkNode *head, LinkNode *tail){
if(head == tail) return ; LinkNode *pivot = partition_iter(head, tail); //copy(v.begin(), v.end(), ostream_iterator
(cout, " ")); qSort(head, pivot); qSort(pivot->next, tail);}int main(int argc, const char **argv){ LinkNode a('c'); LinkNode b('a'); LinkNode c('b'); a.next = &b; b.next = &c; qSort(&a, NULL); LinkNode *p = &a; while (p != NULL) { cout << p->val << " "; p = p->next; } return 0;}#if 0int partition(vector
&v, int head, int tail){ int pivot = v.at(head); int j = head; int i = head + 1; for (; i <= tail; i++) { if(v.at(i) < pivot) { swap(v.at(i), v.at(++j)); } } swap(v.at(head), v.at(j)); return j;}void qSort(vector
&v, int head, int tail){ if(head > tail) return ; int pivot = partition(v, head, tail); //copy(v.begin(), v.end(), ostream_iterator
(cout, " ")); qSort(v, head, pivot - 1); qSort(v, pivot + 1, tail);}int main(int argc, const char **argv){ vector
v { 4, 2, 6, 12, 10, -8, 3, 0}; qSort(v, 0, v.size() - 1); copy(v.begin(), v.end(), ostream_iterator
(cout, " ")); return 0;}#endif#if 0list
::iterator partition_iter(list
::iterator head, list
::iterator tail){ int pivot = *head; list
::iterator j = head; list
::iterator i = next(head); for (; i != tail; i++) { if(*i < pivot) { j++; swap(*i, *j); } } swap(*head, *j); return j;}void qSort(list
::iterator head, list
::iterator tail){ if(head == tail) return ; list
::iterator pivot = partition_iter(head, tail); //copy(v.begin(), v.end(), ostream_iterator
(cout, " ")); qSort( head, pivot); qSort( ++pivot, tail);}int main(int argc, const char **argv){ list
v { 4, 2, 6, 12, 10, -8, 3, 0}; qSort(v.begin(), v.end()); copy(v.begin(), v.end(), ostream_iterator
(cout, " ")); return 0;}#endif

转载地址:http://zrcii.baihongyu.com/

你可能感兴趣的文章
【JAVA数据结构】先进先出队列
查看>>
移植Vim配色方案到Eclipse
查看>>
谈谈加密和混淆吧[转]
查看>>
乘法逆元
查看>>
Objective-C 基础入门(一)
查看>>
SpringCloud学习之PassCloud——(一)PassCloud源代码下载
查看>>
Nginx篇-springCloud配置Gateway+Nginx进行反向代理和负载均衡
查看>>
缓存篇-Redis缓存失效以及解决方案
查看>>
phpquery抓取网站内容简单介绍
查看>>
找工作准备的方向(4月22日写的)
查看>>
关于fwrite写入文件后打开查看是乱码的问题
查看>>
用结构体指针前必须要用malloc,不然会出现段错误
查看>>
Linux系统中的美
查看>>
一些实战项目(linux应用层编程,多线程编程,网络编程)
查看>>
原来k8s docker是用go语言写的,和现在所讲的go是一个东西!
查看>>
STM32CubeMX 真的不要太好用
查看>>
STM32CubeMX介绍、下载与安装
查看>>
不要买铝合金机架的无人机,不耐摔,易变形弯曲。
查看>>
ACfly也是基于FreeRTOS的
查看>>
我发现七月在线的GAAS课程基本都讲到了
查看>>