quicksort 快速排序 quick sort
* Java基本版
package cn.mediamix;
import java.util.LinkedList;
public class QuickSort {
public static void qsort(int[]a) {
_qsort(a, 0, a.length-1);
}
private static void _qsort(int []a, int low, int high) {
if (low >= high) {
return;
}
int first = low, last = high, pivot = a[first];
while (first < last) {
while (first < last && a[last] >= pivot) {
--last;
}
a[first] = a[last];
while (first < last && pivot >= a[first]) {
++first;
}
a[last] = a[first];
}
a[first] = pivot;
_qsort(a, low, first-1);
_qsort(a, first+1, high);
}
public static void main(String[] args) {
int[] a = {9, 1, 4, 7, 8, 0, 6, 5, 2, 3};
qsort(a);
// 测试新的遍历方式
LinkedList<Integer> items = new LinkedList<Integer>();
for (int e : a) {
items.add(new Integer(e));
}
items.forEach(item->System.out.println(item));
}
}
* Java通用版排序
GenericSort.java
package cn.mediamix; import java.util.Comparator;
import java.util.List; public class GenericSort<T> { public static <T> void qsort(List<T> a, Comparator<T> comparator) {
_qsort(a, 0, a.size() - 1, comparator);
} private static <T> void _qsort(List<T> a, int low, int high, Comparator<T> comparator) {
if (low >= high) {
return;
}
int first = low, last = high;
T pivot = a.get(first);
while (first < last) {
while (first < last && comparator.compare(a.get(last), pivot) >= 0) {
--last;
}
a.set(first, a.get(last));
while (first < last && comparator.compare(pivot, a.get(first)) >= 0) {
++first;
}
a.set(last, a.get(first));
}
a.set(first, pivot);
_qsort(a, low, first - 1, comparator);
_qsort(a, first + 1, high, comparator);
} }
TestGeneric.java
package cn.mediamix; import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List; import cn.mediamix.GenericSort; public class TestGeneric {
public static void main(String[] args) {
int[] a = {9, 1, 4, 7, 8, 0, 6, 5, 2, 3};
// int[] => List<Integer>
List<Integer> items = new ArrayList<Integer>(a.length);
for (int e : a) {
items.add(new Integer(e));
}
// my qsort
GenericSort.qsort(items, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return i2-i1; // 从大到小排序
}
});
// traverse
items.forEach(item -> {System.out.println(item);}); // test string
List<String> greet = new LinkedList<String>();
greet.add("Hello");
greet.add("Bonjour");
greet.add("Shalom");
greet.add("Hola");
greet.add("Konnichiwa");
greet.add("Nihao");
GenericSort.qsort(greet, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
greet.forEach(item -> {System.out.println(item);});
} }
Output:
9
8
7
6
5
4
3
2
1
0
Bonjour
Hello
Hola
Konnichiwa
Nihao
Shalom
Output:
参考代码片段
Collections.sort(list, new Comparator<Dog>() { @Override public int compare(Dog o1, Dog o2) { return o2.age - o1.age; } });
@file: java.util.collections
p.p1 { margin: 0; font: 14px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
public class Collections {
// ...
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
//...
}
@file: java.util.List
p.p1 { margin: 0; font: 14px Monaco; color: rgba(57, 51, 255, 1) }
p.p2 { margin: 0; font: 14px Monaco }
span.s1 { color: rgba(0, 0, 0, 1) }
span.s2 { color: rgba(119, 119, 119, 1) }
span.s3 { color: rgba(147, 26, 104, 1) }
span.s4 { color: rgba(126, 80, 79, 1) }
span.s5 { text-decoration: underline }
p.p1 { margin: 0; font: 14px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
public interface List<E> extends Collection<E> {
// ...
@SuppressWarnings({"unchecked", "rawtypes"})
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
// ...
}
@file: java.util.comparator
p.p1 { margin: 0; font: 14px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
public interface Comparator<T> {
// ...
p.p1 { margin: 0; font: 14px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
span.s2 { text-decoration: underline }
span.s3 { color: rgba(126, 80, 79, 1) }
int compare(T o1, T o2);
}
//-------------------------------------------------------------------------//
// 下面的是弱类型的脚本语言: //
// ----------------------------------------------------------------------------------------//
* javascript
// 上一次写错了 first < last 对于下标比较大小,控制下标走动位置,不需要用比较大小的回调函数
/**
* Created by Mch on 8/5/18.
*/
function qsort(a, c) {
c = c || function(a, b) {
return a - b;
};
function _qsort(a, low, high) {
if (low >= high) {
return;
}
var first = low, last = high, pivot = a[first];
while (first < last) {
while (first < last && c(a[last], pivot) >= 0) {
--last;
}
a[first] = a[last];
while (first < last && c(pivot, a[first]) >= 0) {
++first;
}
a[last] = a[first];
}
a[first] = pivot;
_qsort(a, low, first-1);
_qsort(a, first+1, high);
}
return _qsort(a, 0, a.length-1);
}
test:
var a = [9, 1, 4, 7, 8, 0, 6, 5, 2, 3];
qsort(a, function(a, b) {
return b-a;
});
console.log(a); var b = ['Hello', 'wold', 'sange', 'yasha', 'shuriken', 'sword'];
qsort(b, function(a, b) {
return a.localeCompare(b);
});
console.log(b);
output:
[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]
[ 'Hello', 'sange', 'shuriken', 'sword', 'wold', 'yasha' ]
output
* PHP:
http://php.net/manual/en/function.usort.php
bool usort ( array &$array , callable $value_compare_func )
<?php
class QuickSort {
private $compare;
public function __construct($compare) {
if (!$compare) {
$compare = function($a, $b) {
return $a - $b;
};
}
$this->compare = $compare;
}
private function _qsort(&$a, $low, $high) {
if ($low >= $high) {
return;
}
$first = $low;
$last = $high;
$pivot = $a[$first]; $c = $this->compare;
$ge = function($a, $b) use ($c) {
return call_user_func($c, $a, $b) >= 0;
}; while ($first < $last) {
while ($first < $last && $ge($a[$last], $pivot)) {
--$last;
}
$a[$first] = $a[$last];
while ($first < $last && $ge($pivot, $a[$first])) {
++$first;
}
$a[$last] = $a[$first];
}
$a[$first] = $pivot;
self::_qsort($a, $low, $first-1);
self::_qsort($a, $first+1, $high);
}
public function qsort(&$a) {
self::_qsort($a, 0, count($a)-1);
}
}
test:
<?php
// test: "Hello world Blog Control" 字符串按空格分隔,分成数组,转化为小写,按字母顺序排
$a = explode(" ", "Hello world Blog Control");
$a = array_map(function($e) {
return strtolower($e);
}, $a);
$q = new QuickSort(function($a, $b) {
return strcmp($a, $b);
});
$q->qsort($a); // 这里用call_user_func($q->qsort, $a)不起作用?
print_r($a);
Output:
Array
(
[0] => blog
[1] => control
[2] => hello
[3] => world
)
快速排序算法
p.p1 { margin: 0; font: 14px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
span.s2 { color: rgba(126, 80, 79, 1) }
quicksort 快速排序 quick sort的更多相关文章
- [算法] 快速排序 Quick Sort
快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- 快速排序Quick sort
快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...
- Java中的经典算法之快速排序(Quick Sort)
Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...
- 排序算法 - 快速排序(Quick Sort)
算法思想 快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod). (1) 分治法的基本思想 ...
- 基础算法之快速排序Quick Sort
原理 快速排序(Quicksort)是对冒泡排序的一种改进. 从数列中挑出一个元素,称为"基准"(pivot); 排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的 ...
- 快速排序算法回顾 --冒泡排序Bubble Sort和快速排序Quick Sort(Python实现)
冒泡排序的过程是首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换,然后比较第二个记录和第三个记录的关键字.以此类推,直至第n-1个记录和第n个记录的关键字进行过比较为止 ...
- 快速排序——Quick Sort
基本思想:(分治) 先从数列中取出一个数作为key值: 将比这个数小的数全部放在它的左边,大于或等于它的数全部放在它的右边: 对左右两个小数列重复第二步,直至各区间只有1个数. 辅助理解:挖坑填数 初 ...
- 快速排序 Quick Sort
自己写的代码,记录一下.分别记录了两种partition的方法. public class QuickSort { public static void quickSort(int[] nums, i ...
随机推荐
- VLAN-1 基础配置及access接口
一.实验拓扑图 二.实验编制 三.实验步骤 1.给对应的PC设置对应的IP和掩码还有接口,以及根据需要划分不同的vlan区域,再用文本标记出不同部门. 2.启动设备(全选) 3.首先用ping命令检查 ...
- HTML5内嵌文本编辑器
1.这个编辑器用的是KindEditor 先看下效果: 2.准备: a):从官网下载KindEditor--->http://kindeditor.net/down.php b):解压到桌面测试 ...
- luoguP1528&2329 栅栏&切蛋糕
前言 蒟弱本来是在亿万年前做二分答案专题栅栏的,由于数据水所以过掉了,后来发现有一个数据加强版,也就是本题,于是爆T了...过了有个五六个月回来填坑了...现在开O2是在最优解第一个(自豪ing 题目 ...
- mpvue学习笔记
坑一: 挂载在Vue.prototype上的属性,在模板语法里面是undefined,必须经过computed计算过一下才能用. 坑二: 关于生命周期钩子 因为小程序的历史页面不会销毁,所以在生命周期 ...
- LeetCoded第20题题解--有效的括号
有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...
- [SWMM]模型子汇水区划分的几种方法
子汇水区的划分是SWMM模型建模的主要步骤之一,划分的好坏对结果精度有比较大的影响.概括来讲,子汇水区的划分有以下几种思路: (1)根据管网走向.建筑物和街道分布,直接人工划分子汇水区.这个方法适用于 ...
- Struts2之国际化
时间:2017-1-11 11:12 --国际化Struts2已经对国际化进行了封装,我们只需要根据其提供的API进行访问即可.要使用国际化的Action必须继承ActionSupport.1.什么是 ...
- Python如何读写Excel文件-使用xlrd/xlwt模块
时间: 2020-08-18 整理: qiyuan 安装和导入 1.模块介绍 在 python 中使用 xlrd/xlwt 和 openpyxl 模块可以对Excel电子表格(xls.xlsx文件)进 ...
- 用C++实现的增强Eratosthenes筛法程序
运行示例 PS H:\Read\num\x64\Release> .\eSievePro Eratosthenes sieve: a method to find out all primes ...
- 前端性能优化(四)——网页加载更快的N种方式
网站前端的用户体验,决定了用户是否想要继续使用网站以及网站的其他功能,网站的用户体验佳,可留住更多的用户.除此之外,前端优化得好,还可以为企业节约成本.那么我们应该如何对我们前端的页面进行性能优化呢? ...