* 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的更多相关文章

  1. [算法] 快速排序 Quick Sort

    快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...

  2. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

  3. 快速排序Quick sort

    快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...

  4. Java中的经典算法之快速排序(Quick Sort)

    Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...

  5. 排序算法 - 快速排序(Quick Sort)

    算法思想 快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod). (1) 分治法的基本思想  ...

  6. 基础算法之快速排序Quick Sort

    原理 快速排序(Quicksort)是对冒泡排序的一种改进. 从数列中挑出一个元素,称为"基准"(pivot); 排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的 ...

  7. 快速排序算法回顾 --冒泡排序Bubble Sort和快速排序Quick Sort(Python实现)

    冒泡排序的过程是首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换,然后比较第二个记录和第三个记录的关键字.以此类推,直至第n-1个记录和第n个记录的关键字进行过比较为止 ...

  8. 快速排序——Quick Sort

    基本思想:(分治) 先从数列中取出一个数作为key值: 将比这个数小的数全部放在它的左边,大于或等于它的数全部放在它的右边: 对左右两个小数列重复第二步,直至各区间只有1个数. 辅助理解:挖坑填数 初 ...

  9. 快速排序 Quick Sort

    自己写的代码,记录一下.分别记录了两种partition的方法. public class QuickSort { public static void quickSort(int[] nums, i ...

随机推荐

  1. sqli-labs lesson5-6 布尔盲注 报错注入 延时注入

    LESSON 5: 典型的布尔盲注. 盲注:sql注入过程中,sql语句的执行结果不回显到前端,这个时候就只能用一些别的方法进行判断或者尝试,这个判断或者尝试就叫做盲注.盲注又分为:1.基于布尔SQL ...

  2. SQL 练习21

    查询每门课程被选修的学生数 SELECT cid,COUNT(cid) 选修人数 from sc GROUP BY cid

  3. Groovy+Spock单元测试

    一.导入依赖 Spock是基于JUnit的单测框架,提供一些更好的语法,结合Groovy语言,可以写出更为简洁的单测. <!-- groovy依赖 --> <dependency&g ...

  4. windows10磁盘分区后,如何恢复分区,回到未分区之前

    windows10磁盘分区后,恢复到分区以前的状态 1.我的电脑右键======>管理 2.找到磁盘管理 3.因为我的H盘原来是和F盘是同一个分区,只是拆分出来了,所有,找到H盘(确保数据都做过 ...

  5. Qt迭代器(Java类型和STL类型)详解

    迭代器为访问容器类里的数据项提供了统一的方法,Qt 有两种迭代器类:Java 类型的迭代器和 STL 类型的迭代器. 两者比较,Java 类型的迭代器更易于使用,且提供一些高级功能,而 STL 类型的 ...

  6. 八:Filter(过滤器)

    一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...

  7. leaflet获取arcgis服务图层所有信息

    L.esri.query({ url: "http://127.0.0.1:6080/arcgis/rest/services/demo/ditu/MapServer/0" }). ...

  8. Git中使用.gitignore忽略文件的推送

    转载自:https://blog.csdn.net/lk142500/article/details/82869018 windows下可以用另存为生成gitignore 文件 1 简介 在使用Git ...

  9. SpringCloud分布式配置中心Config

    统一管理所有配置. 1.微服务下的分布式配置中心 简介:讲解什么是配置中心及使用前后的好处 什么是配置中心: 一句话:统一管理配置, 快速切换各个环境的配置 相关产品: 百度的disconf 地址:h ...

  10. 设置 Qt GUI程序 printf输出到独立控制台