Data Structure Linked List: Merge Sort for Linked Lists
http://www.geeksforgeeks.org/merge-sort-for-linked-list/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *next;
node() : data(), next(NULL) { }
node(int d) : data(d), next(NULL) { }
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->next = head;
head = new_node;
} void print(node* head) {
if (!head) return;
cout << head->data << " ";
print(head->next);
} void frontbacksplit(node *head, node *&a, node *&b) {
node *p, *q;
if (!head || !head->next) {
a = head;
b = NULL;
return;
}
p = head;
q = head->next;
while (q) {
q = q->next;
if (q) {
q = q->next;
p = p->next;
}
}
a = head;
b = p->next;
p->next = NULL;
} node *sortmerge(node *a, node *b) {
node *ans = NULL;
if (!a) return b;
if (!b) return a;
if (a->data < b->data) {
ans = a;
ans->next = sortmerge(a->next, b);
}
else {
ans = b;
ans->next = sortmerge(a, b->next);
}
return ans;
} void mergesort(node *&head) {
if (!head || !head->next) return;
node *a, *b;
node *h = head;
frontbacksplit(h, a, b);
mergesort(a);
mergesort(b);
head = sortmerge(a, b);
} int main() {
node *head = NULL;
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
mergesort(head);
print(head);
return ;
}
Data Structure Linked List: Merge Sort for Linked Lists的更多相关文章
- [Linked List]Insertion Sort List
Total Accepted: 59422 Total Submissions: 213019 Difficulty: Medium Sort a linked list using insertio ...
- LeetCode -- Merge Two Sorted Linked List
Question: Merge two sorted linked lists and return it as a new list. The new list should be made by ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- 【推荐】Data Structure Visualizations
University of San Francisco David Galles 功能:可视化数据结构&算法实现过程 网站地址 https://www.cs.usfca.edu/~ga ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- sicily 1154. Easy sort (tree sort& merge sort)
Description You know sorting is very important. And this easy problem is: Given you an array with N ...
随机推荐
- Android NDK开发篇(四):Java与原生代码通信(原生方法声明与定义与数据类型)
Java与原生代码通信涉及到原生方法声明与定义.数据类型.引用数据类型操作.NIO操作.訪问域.异常处理.原生线程 1.原生方法声明与定义 关于原生方法的声明与定义在上一篇已经讲一点了,这次具体分析一 ...
- zabbix自动发现监控远程端口
zabbix监控远程服务器端口,simple checks是zabbix用来监控无agent的主机 脚本和模板地址: https://github.com/mikeluwen/tcpmonitor
- windows和linux下目录分隔符兼容问题(换行回车兼容)
windows和linux下目录分隔符兼容 DIRECTORY_SEPARATOR 换行回车兼容 PHP_EOF
- servlet文件下载2(单文件下载和批量下载)
使用servlet完毕单文件下载和批量文件下载.批量下载的原理是先将文件打包成zip , 然后再下载. 之前也转载过一篇文件下载的博客,地址:http://blog.csdn.net/ch717828 ...
- Junit使用教程(三)
四.实例总结 1. 参数化测试 有时一个测试方法,不同的参数值会产生不同的结果,那么我们为了测试全面,会把多个参数值都写出来并一一断言测试,这样有时难免费时费力,这是我们便可以采用参数化测试来解决这个 ...
- iOS 解决TableView reloadData时cell中图片会闪的问题
tableView调用reloaddata的时候发现有个小问题,每次刷新图片都会抖动闪烁一下,看着很难受,也影响体验.造成这个问题的主要原因是因为刷新时候切换图片导致.要解决这个问题也很好解决,使用S ...
- 快速搭建一个成熟,强壮的App框架【转载】
App框架搭建 招聘信息: iOS 研发工程师 iOS开发工程师 iOS开发实习工程师 新浪微博-Android开发工程师&iOS开发工程师 美术设计师(2D) UI设计师 cocos2dx手 ...
- VS2012删除选项卡菜单中的"关闭所有文档"
delete the "close all documents" item of tab menu in vs2012 Tools -> Customize -> Co ...
- 解决UISlider滑块不灵敏
由于UI给的thumbImage图片过小,默认UISlider开始拖动的手势范围只有thumbImage的大小之内. 为了解决这个问题需要创建一个子类继承于UISlider.重写其中的方法: - (C ...
- Cocoa 静态显示一个对话框
M // // form2.m // test_multi_window // // Created by on 23/7/14. // Copyright (c) 2014 EDU. All rig ...