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 ...
随机推荐
- Elasticsearch BM25相关度算法超详细解释
Photo by Pixabay from Pexels 前言:日常在使用Elasticsearch的搜索业务中多少会出现几次 "为什么这个Doc分数要比那个要稍微低一点?".&q ...
- luoguP2601 对称的正方形
题目描述 给出一个数字矩形,求这个矩形中有多少个子正方形满足上下对称.左右对称. 思路 我们可以用3个哈希数组 \(a\ b\ c\) 分别表示矩形从左上往右下看,从左下往右上看,从右上往左下看的样子 ...
- kubebuilder实战之四:operator需求说明和设计
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- spring开发中常见错误集合,逐步添加
1.关于jstl错误:原因,在jsp页面中使用了jstl标签库,但是却没有导入,可以将相应的jar包放在tomcat的lib目录下,一劳永逸 Java.lang.NoClassDefFoundErro ...
- .Net Core 踩坑记录--程序独立发布 无法运行
背景 创建.net Core3.1 的Console程序 点击发布 选择独立部署模式 目标电脑 Win10 x64 未安装任何.Net SDK 现象 发布的程序 点击运行没有反应 或是直接闪退 解决 ...
- 【springcloud】Zuul 超时、重试、并发参数设置
转自:https://blog.csdn.net/xx326664162/article/details/83625104 一. Zuul 服务网关 服务网关 = 路由转发 + 过滤器 1.路由转发: ...
- 在localStorage中存储对象数组并读取
频繁ajax请求导致页面响应变慢. 于是考虑将数据存储在window.storage中,这样只请求一次ajax,而不需要频繁请求. 鉴于localstorage中只能存储字符串,所以我们要借助于JSO ...
- bootStrap模态框与select2合用时input不能获取焦点、模态框内部滑动,select选中跳转
bootStrap模态框与select2合用时input不能获取焦点 在bootstrap的模态框里使用select2插件,会导致select2里的input输入框没有办法获得焦点,没有办法输入. 把 ...
- CentOS 6.x 系统中安装原生 Hadoop 2
2020年整理博客发现原文地址已经失效,推荐学习地址厦门大学数据库实验室 本教程适合于在 CentOS 6.x 系统中安装原生 Hadoop 2,适用于Hadoop 2.7.1, Hadoop 2.6 ...
- Python 3.10 is coming!
看看Python 官网的文档 whatsnew,Python 3.10 已然距离我们越来越近了,然我们看看 Python 3.10 相较于 Python 3.9 有哪些改变吧 新特性 通过括号来组织多 ...