题目来源。待字闺中。原创@陈利人
。欢迎大家继续关注微信公众账号“待字闺中”

分析:思路和数据的高速排序一样,都须要找到一个pivot元素、或者节点。

然后将数组或者单向链表划分为两个部分。然后递归分别快排。

针对数组进行快排的时候,交换交换不同位置的数值。在分而治之完毕之后,数据就是排序好的。那么单向链表是什么样的情况呢?除了交换节点值之外。是否有其它更好的方法呢?能够改动指针,不进行数值交换。这能够获取更高的效率。

在改动指针的过程中。会产生新的头指针以及尾指针,要记录下来。在partition之后,要将小于pivot的的部分、pivot、以及大于pivot的部分又一次串起来成为一个singly
linked list。

在partition时。我们用最后的节点作为pivot。当我们扫描链表时,假设节点值大于pivot,将节点移到尾部之后;假设节点小于。保持不变。

在递归排序时。我们先调用partition将pivot放到正确的为止并返回pivot,然后。递归左边。递归右边。最后在合成一个单链表。

详细代码例如以下:

#include <iostream>
#include <map>
#include <vector>
#include <assert.h>
using namespace std; struct node
{
int data;
struct node* next;
node(int x):data(x),next(NULL){}
}; node* partition(node* start,node* end,node** newHead,node** newEnd)
{
node* provit = end,*tmp = NULL,*pre = NULL;
while(start != provit)
{
if(start->data > provit->data)
{
tmp = start->next;
if(pre)pre->next = tmp;
start->next = NULL;
end->next = start;
end = start;
start = tmp;
}
else
{
if(*newHead == NULL)*newHead = start;
pre = start;
start = start->next;
}
}
if(*newHead == NULL)*newHead = provit;
*newEnd = end;
return provit;
}
node* GetTail(node* head)
{
if(head == NULL)return NULL;
while(head->next != NULL)head = head->next;
return head;
}
node* QuickSort(node* head,node* end)
{
if(head == NULL || head == end)return head;
node* newHead = NULL,*newEnd = NULL;//此处不能用二维指针。不然partition调用*newHead解引用时会出错
node* provit = partition(head,end,&newHead,&newEnd);
if(newHead != provit)
{
node* tmp = newHead;
while(tmp->next != provit)tmp = tmp->next;
tmp -> next = NULL;
newHead = QuickSort(newHead,tmp);
tmp = GetTail(newHead);
tmp->next = provit;
}
if(provit != newEnd)
{
provit->next= QuickSort(provit->next,newEnd);
}
return newHead;
}
void QuickSort(node** head)
{
if(head == NULL)return;
*head = QuickSort(*head,GetTail(*head));
}
void printLink(node* head)
{
while(head != NULL)
{
cout << head->data <<" ";
head = head->next;
}
cout << endl;
}
int main()
{
int n,i,value;
node* head,*q;
cin >> n;
for(i=0;i<n;i++)
{
cin >> value;
if(i == 0)
{
head = new node(value);
q = head;
}
else
{
node* p = new node(value);
q->next = p;
q = p;
}
}
printLink(head);
QuickSort(&head);
printLink(head);
return 0;
}

leetcode之

Sort List

Sort
a linked list in O(n log n)
time using constant space complexity.

该题用上面的方法会超时,能够使用归并排序

class Solution {
public:
ListNode *sortList(ListNode *head)
{
if(!head || !head->next)return head;
ListNode* fast = head -> next -> next;//至少有两个节点
ListNode* slow = head;
while(fast)
{
fast = fast->next;
slow = slow->next;
if(!fast)break;
fast = fast->next;
}
ListNode* p = slow -> next;
slow -> next = NULL;
ListNode* q = sortList(head);
p = sortList(p);
head = NULL;
ListNode* tail = NULL;
while(q && p)
{
if(q->val > p->val)
{
if(!head)head = tail = p;
else
{
tail->next = p;
tail = tail->next;
}
p = p->next;
}
else
{
if(!head)head = tail = q;
else
{
tail->next = q;
tail = tail->next;
}
q = q->next;
}
}
if(p)
{
if(!head)head = tail = p;
tail->next = p;
}
if(q)
{
if(!head)head = tail = q;
else tail->next = q;
}
return head;
}
};

待字闺中之快排单向链表;leetcode之Sort List的更多相关文章

  1. 链表快排 & 基于链表的排序

    以前只知道链表做插入(朴素.非二分)排序挺方便的.现在知道了(单)链表进行快速排序也是很好的(只是跟一般的快排的方式不一样). 参考: http://blog.csdn.net/otuhacker/a ...

  2. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  3. 结构体快排回顾(sort)

    一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include < ...

  4. C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...

  5. 63.如何对单链表进行快排?和数组快排的分析与对比[quicksort of array and linked list]

    [本文链接] http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html [题目] 单链表的特点是:单向. ...

  6. java链表实现快排

    链表文件 package sort; public class SqList {    public int LIST_INIT_SIZE = 8;//链表的原始大小    private int I ...

  7. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  8. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  9. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

随机推荐

  1. selenium3+python自动化50-环境搭建(firefox)

    前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...

  2. ubuntu 常用配置

    root 登录 sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf加:greeter-show-manual-login=true设 ...

  3. 26个Jquery1.4使用小技巧

    1. 禁止右键点击 1.       $(document).ready(function(){ 2.           $(document).bind("contextmenu&quo ...

  4. PhpSpreadsheet生成Excel时实现单元格自动换行

    PhpSpreadsheet是PHPExcel的替代版本,PHPExcel的作者已经停止了更新,今天尝试了使用PhpSpreadsheet生成Excel的时候支持单元格内的自动换行,发现用法其实差不多 ...

  5. 实现Hadoop的Writable接口Implementing Writable interface of Hadoop

    As we saw in the previous posts, Hadoop makes an heavy use of network transmissions for executing it ...

  6. Common Internet File System

    CIFS (Common Internet File System) is a protocol that gained popularity around the year 2000, as ven ...

  7. C++的Vector用法

    转自:http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. ...

  8. 基于zabbix 的memached 多实例监控

    基于zabbix 的memached 多实例监控 zabbix agentd 配置文件新增配置: UserParameter=memcached.server.discovery[*],ps uax ...

  9. jQuery多媒体播放器插件jQuery Media Plugin使用方法

    jQuery Media Plugin是一款基于jQuery的网页媒体播放器插件,它支持大部分的网络多媒体播放器和多媒体格式,比如:Flash, Windows Media Player, Real ...

  10. C# 特性(Attribute)(一)

    特性(Attributes)是一种崭新的声明性信息.我们不仅可以通过特性来定义设计层面的信息(例如help file, URL for documentation)以及运行时(run-time)信息( ...