c语言双向链表 快速排序,双向链表的快速排序(swift版本)
面试经常会被问到的单向链表的快速排序or双向链表的快速排序,现在用swift写了一个双向链表的快速排序,直接上代码
获取源码
//初始化
var linkList = LinkList()
linkList.append(value: 12)
linkList.append(value: 5)
linkList.append(value: 30)
linkList.append(value: 3)
linkList.append(value: 3)
linkList.append(value: 2)
linkList.append(value: 9)
linkList.append(value: 4)
linkList.append(value: 11)
linkList.append(value: 19)
linkList.append(value: 15)
linkList.append(value: 16)
print("Init Data: \(linkList)")
var firstNode = linkList.searchIndex(index: 0)
var endNode = linkList.searchIndex(index: linkList.linkListSize() - 1)
quickSort(linkList: &linkList, low: &firstNode, high: &endNode)
print("After QuickSort: \(linkList)")
// Print效果
Init Data: [Optional(12), Optional(5), Optional(30), Optional(3), Optional(3), Optional(2), Optional(9), Optional(4), Optional(11), Optional(19), Optional(15), Optional(16)]
After QuickSort: [Optional(2), Optional(3), Optional(3), Optional(4), Optional(5), Optional(9), Optional(11), Optional(12), Optional(15), Optional(16), Optional(19), Optional(30)]
Program ended with exit code: 0
class Node {
var value: T
var next: Node?
weak var previous: Node?
init(value: T) {
self.value = value
}
}
class LinkList {
var head: Node?
var tail: Node?
// Add
func append(value: T) {
let newNode = Node.init(value: value)
if let tailNode = tail {
newNode.previous = tailNode
tailNode.next = newNode
}
else {
head = newNode
}
tail = newNode
}
// Size
func linkListSize() -> Int {
if let node = head {
var index = 1
var node = node.next
while node != nil {
index += 1
node = node?.next
}
return index
}
else {
return 0
}
}
// Search
func searchNode(indexNode: Node) -> Int {
if let node = head {
if node === indexNode {
return 0
}
else {
var index: Int = 0
var node = node.next
while node != nil {
index += 1
if node === indexNode {
return index
}
node = node?.next
}
// 不存在返回-1
return -1
}
}
else {
// 不存在返回-1
return -1
}
}
func lowBeforeHigh(low: Node, high: Node) -> Bool {
if low === high {
return false
}
else {
var node = low.next
while node != nil {
if node === high {
return true
}
node = node?.next
}
}
return false
}
func searchIndex(index: Int) -> Node? {
if let node = head {
if index == 0 {
return node
}
else {
var node = node.next
var nodeIndex: Int = 1
while node != nil {
if nodeIndex == index {
return node
}
nodeIndex += 1
node = node?.next
}
return nil
}
}
else {
return nil
}
}
// Remove
func remove(node: Node) -> T {
let preNode = node.previous
let nextNode = node.next
if let preNode = preNode {
// 前节点存在
preNode.next = nextNode
}
else {
// 不存在前节点,将nextNode置成head
head = nextNode
}
nextNode?.previous = preNode
if nextNode == nil {
tail = preNode
}
// 将node置成nil
node.previous = nil
node.next = nil
return node.value
}
func removeAll() {
head = nil
tail = nil
}
}
//MARK: - QuickSort
func quickSort(linkList: inout LinkList, low: inout Node?, high: inout Node?) {
guard linkList.head != nil && linkList.tail != nil else {
return
}
guard low != nil && high != nil else {
return
}
guard linkList.lowBeforeHigh(low: low!, high: high!) else {
return
}
let midIndex = partition(linkList: &linkList, low: &low!, high: &high!)
// 递归
quickSort(linkList: &linkList, low: &low, high: &linkList.searchIndex(index: midIndex )!.previous)
quickSort(linkList: &linkList, low: &linkList.searchIndex(index: midIndex)!.next, high: &high)
}
func partition(linkList: inout LinkList, low: inout Node, high: inout Node) -> Int {
var value: Int = 0
var lowNode = low
var highNode = high
let lowValue = low.value
while linkList.lowBeforeHigh(low: lowNode, high: highNode) {
// 从右边向左边扫描
while linkList.lowBeforeHigh(low: lowNode, high: highNode) && highNode.value >= lowValue {
highNode = highNode.previous!
}
if highNode === lowNode {
value = linkList.searchNode(indexNode: lowNode)
break
}
// lowNode和highNode交换值
let temp1Value = lowNode.value
lowNode.value = highNode.value
highNode.value = temp1Value
// 从左边向右边扫描
while linkList.lowBeforeHigh(low: lowNode, high: highNode) && lowNode.value <= lowValue {
lowNode = lowNode.next!
}
if lowNode === highNode {
value = linkList.searchNode(indexNode: lowNode)
break
}
// lowNode和highNode交换值
let temp2Value = lowNode.value
lowNode.value = highNode.value
highNode.value = temp2Value
}
return value
}
func swapTwoNode(low: inout Node, high: inout Node) {
// 相邻节点
if low.next === high {
low.previous?.next = high
high.previous = low.previous
low.previous = high
low.next = high.next
high.next?.previous = low
high.next = low
}
else {
// 非相邻节点
low.previous?.next = high
low.next?.previous = high
high.next?.previous = low
high.previous?.next = low
let temp1 = low.previous
low.previous = high.previous
high.previous = temp1
let temp2 = low.next
low.next = high.next
high.next = temp2
}
}
c语言双向链表 快速排序,双向链表的快速排序(swift版本)相关推荐
- C语言程序设计之标准库快速排序qsort函数用法示例
C语言程序设计之标准库快速排序qsort函数,排序效率高,使用方便,太棒了. qsort函数定义如下: #include <stdlib.h>void qsort(void *base, ...
- 这是一个php中快速排序字母,php 快速排序
快速排序是以其中一个数为比较标准,其他比较的数分块处理,应用递归按相同想法处理数据 比如:4 3 6 2 1 7 8 以4为比较对象 排序为 3 2 1 6 7 8 左边为 3 2 1 右边 为 6 ...
- python快速排序解析_快速排序python实现总结
算法 数据结构 快速排序python实现总结 背景:数据结构与算法是IT相关的工程师一直以来的基础考察重点,很多经典书籍都是用c++或者java来实现,出于对python编码效率的喜爱,于是取sear ...
- Swift版本都到5了,ABI稳定了,你清楚吗
一.ABl是什么 每个操作系统都会为运行在该系统下的应用程序提供应用程序二进制接口(Application Binary Interface,即ABI), 描述了应用程序和操作系统之间的底层接口,一个 ...
- swift 3迁移到swift5 swift版本迁移指南
Swift 5已经发布下面是官方swift5的介绍. Introducing Swift 5.1 Swift 5.1 now makes it easier to create and share b ...
- 简陋版C语言仿真通讯录之动态内存开辟版本
简陋版C语言仿真通讯录 https://blog.csdn.net/csdn_kou/article/details/80287640 简陋版C语言仿真通讯录之动态内存开辟版本 给Contact结构体 ...
- 使用swiftenv管理swift版本
swiftenv可以让你轻松安装和快速切换在多个版本之间切换的工具 安装swiftenv 1.下载swifenv git clone https://github.com/kylef/swiftenv ...
- MacOS Xcode与Swift版本对应关系
Swift4 Xcode9.0 or later macOS 10.12.6+ Xcode 9.4.1 macOS 10.13.2+ ...
- ios使用SARUnArchiveANY 解压rar文件(oc和swift版本)
SARUnArchiveANY简介 开源库网址: https://github.com/saru2020/SARUnArchiveANY 简介: 一个iOS的非常有用的库来解压zip,.rar,7z文 ...
最新文章
- 推荐算法炼丹笔记:阿里序列化推荐算法MRIF
- 用户资源管理DBMS_RESOURCE_MANAGER
- android 环境配置和安装, Android系统包说明,基本控件,常用代码,ADB 命令行,APK文件确解,小技艺,...
- SAP 那点事BW HANA
- boot lvm 分区_Linux如何在线对逻辑分区扩容
- 【kafka】kafka 新增节点 报错 InconsistentBrokerIdException Configured broker.id doesn‘t match
- 小程序入门学习17--springboot环境配置01
- 这个机器学习论文大众评审网站,要让每篇arXiv论文都得到讨论
- java 判断字符串是否是整数
- Adaboost新理解
- 用户名_网络安全与防御-OpenSSH用户名枚举及其检测方法
- acr122ua9读写软件_NFC读卡器ACR122U-A9 非接触型IC读写器 龙杰读卡器 IC解密器
- H3C交换机定时重启配置方法
- Android 悬浮窗的实现(类似于360加速球)
- 颜色的前世今生14·RGB拾色器详解
- C#实现简单的加密防止拷贝复制软件
- php chm中文手册201502月更新
- 图文详解win7声音图标不见了怎么办
- 流体动力学模拟软件Realflow教程,Realflow水花飞溅特效/粒子特效等特效入门教程
- 跨境茶话会8月期丨性能优化的艺术
热门文章
- CentOS 7 安装MongoDB 4.0
- Could not open ServletContext resource [/WEB-INF/applicationContext.xml]”解决方案
- 基于应用层自身反远程线程注入的研究
- 你的Windows Phone 手机还没有升级7.5吗?我有办法,亲身体验!
- 动态规划|Dynamic Programming
- python3中property使用方法详解
- “unauthorized: authentication required” -- openshift3.9 docker push 报错
- 数据结构 链式哈希表(Hash Table)的接口定义与实现分析(完整代码)
- Hibernate 学习-1
- [转载]监控 Linux 性能的 18 个命令行工具