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 ...
随机推荐
- Java之JSTL标签与JavaBean
Java之JSTL标签与JavaBean JSP.JSTL标签及 EL表达式 <!-- jstl表达式--> <dependency> <groupId>javax ...
- 数据结构与算法-排序(九)基数排序(Radix Sort)
摘要 基数排序是进行整数序列的排序,它是将整数从个位开始,直到最大数的最后一位截止,每一个进位(比如个位.十位.百位)的数进行排序比较. 每个进位做的排序比较是用计数排序的方式处理,所以基数排序离不开 ...
- HTML5内嵌文本编辑器
1.这个编辑器用的是KindEditor 先看下效果: 2.准备: a):从官网下载KindEditor--->http://kindeditor.net/down.php b):解压到桌面测试 ...
- Nginx-出现-403-Forbidden
步骤一: 检查目录权限.权限不足的就加个权限吧. 例子:chmod -R 755 / var/www 步骤二: 打开nginx.conf 例子:vim /etc/nginx/nginx.conf 把 ...
- vue路由history模式,nginx配置
nginx配置内容 # For more information on configuration, see: # * Official English Documentation: http://n ...
- SpringBoot自定义请求参数转换器
需求 我们可能对接客户的系统的时候,虽然Spring为我们提供的很多方便的转换器,但是遇到还是可能遇到需要自定义请求参数转换器的情况. 日期转换器 SpringBoot默认是没有配置日期转换器的我们可 ...
- Qt元对象和属性系统详解
Qt 是一个用标准 C++ 编写的跨平台开发类库,它对标准 C++ 进行了扩展,引入了元对象系统.信号与槽.属性等特性,使应用程序的开发变得更高效. 本节将介绍 Qt 的这些核心特点,对于理解和编写高 ...
- 在多数据源中对部分数据表使用shardingsphere进行分库分表
背景 近期在项目中需要使用多数据源,其中有一些表的数据量比较大,需要对其进行分库分表:而其他数据表数据量比较正常,单表就可以. 项目中可能使用其他组的数据源数据,因此需要多数据源支持. 经过调研多数据 ...
- bt面板安装邮局系统
前些日子阿里云优惠就顺便买了个服务器,今天想在阿里云的服务器上试着安装一个邮件服务,突然发现之前安装的好好的邮件服务插件不能正常安装了,一直报错. 点击该链接享受本文章的纯净无广告版 查看了下出错的地 ...
- node十年心酸史,带你了解大前端的由来!
前言 近年来,随着前端的丰富,前后端分离是趋势.各种东西如雨后春笋一般,层出不穷.node.js的出现,使前端真正意义上变成了大前端. 前端由来之HTML发展史 1990 年,Tim Berners- ...