博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
交互设计算法基础(3) - Quick Sort
阅读量:4453 次
发布时间:2019-06-08

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

1 int pivotIndex, pivot, swapIndex; 2  3 void swap(int[] arr, int x, int y) { 4   int temp = arr[x]; 5   arr[x] = arr[y]; 6   arr[y] = temp; 7 } 8  9 void quickSort(int[] arr, int start, int end) {10   if (end <= start) return;11 12   pivotIndex = (start + end)/2;13   pivot = arr[pivotIndex];14   swap(arr, pivotIndex, end);15   swapIndex = start;16   for (int i = start; i < end; i++) {17     if (arr[i] <= pivot) {18       swap(arr, i, swapIndex);19       ++swapIndex;20     }21   }22   swap(arr, swapIndex, end);23 24   quickSort(arr, start, swapIndex-1);25   quickSort(arr, swapIndex+1, end);26 } 27 28 void draw() {29   noLoop();30   int[] arr = {10, 5, 2, 3};31   quickSort(arr, 0, arr.length-1);32   println(arr);33 }

快速排序,说白了就是快啦,不过有两种实现方式,一种普通,一种In-place,后面的比前面的占用较少空间。

快排用分治法解决。

最佳时间复杂度:O(nlog n)

平均时间复杂度:O(nlog n)

最差时间复杂度:O(n2)

空间复杂度:一般版本O(n),In-place O(log n)

转载于:https://www.cnblogs.com/x5115x/p/7530765.html

你可能感兴趣的文章
Content Server HA搭建
查看>>
vue-textarea 自适应高度
查看>>
(2)数据结构——线性表(链表)实现
查看>>
[leetCode]Linked List Cycle I+II
查看>>
leetcode中的python学习
查看>>
sqlserver打开对象资源管理器管理的帮助文档的快捷键
查看>>
JBOSSAS 5.x/6.x 反序列化命令执行漏洞(CVE-2017-12149)
查看>>
Zookeeper zkui-zookeeper图形化管理工具
查看>>
java运行时内存分类
查看>>
为什么说 Git 比 SVN 更好
查看>>
1.基础数据类型的初识 字符串 bool 整型 if else elif
查看>>
【设计模式】4、原型模式
查看>>
进入meta模式关闭背光灯
查看>>
webstorm上svn的安装使用
查看>>
【JEECG技术文档】数据权限自定义SQL表达式用法说明
查看>>
使用 Bootstrap Typeahead 组件
查看>>
EF不能很好的支持DDD?估计是我们搞错了!
查看>>
Qt 静态库与共享库(动态库)共享配置的一个小办法
查看>>
linux_cacti 配置之 安装snmp 服务
查看>>
201407-至今
查看>>