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));
	}
}

  

* javascript

function qsort(a, comp) {
	if (typeof comp==="undefined") {
		comp = function(a, b) {
			return a - b < 0;
		}
	}
	function _qsort(a, low, high) {
		if (!comp(low, high)) {
			return;
		}
		var first = low, 
			last = high,
			pivot = a[first];
		while (comp(first, last)) {
			while (comp(first, last) && !comp(a[last], pivot)) {
				--last;
			}
			a[first] = a[last];
			while (comp(first, last) && !comp(pivot, a[first])) {
				++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);
}

// quicksort
var a = [57, 68, 59, 52, 52, 72, 28, 96, 33, 24];
qsort(a);   // 传参数回调不起作用 ?
console.log(a); // [24, 28, 33, 52, 52, 57, 59, 68, 72, 96]

  

* PHP:

http://php.net/manual/en/function.usort.php

bool usort ( array &$array , callable $value_compare_func )

  

<?php
/**
 * Created by PhpStorm.
 * User: Mch
 * Date: 7/7/18
 * Time: 15:40
 */
class MyArray {
    private $a = [];

    public function __construct($a) {
        $this->a = $a;
    }

    public static function _qsort(&$a, $low, $high, $lt) {
        if (!$lt($low, $high)) {
            return;
        }
        $first = $low;
        $last = $high;
        $pivot = $a[$first];
        while ($lt($first, $last)) {
            while ($lt($first, $last) && !$lt($a[$last], $pivot)) {
                --$last;
            }
            $a[$first] = $a[$last];
            while ($lt($first, $last) && !$lt($pivot, $a[$first])) {
                ++$first;
            }
            $a[$last] = $a[$first];
        }
        $a[$first] = $pivot;
        self::_qsort($a, $low, $first - 1, $lt);
        self::_qsort($a, $first + 1, $high, $lt);
    }

    // @param: $c  compare
    public function qsort($c = NULL) {

        if (NULL === $c) {
            $c = function ($a, $b) {
                return $a - $b;
            };
        }
        // @ref: http://www.php.net/manual/en/class.closure.php
        $lt = function ($a, $b) use ($c) {
            // @ref: http://www.php.net/manual/en/function.call-user-func.php
            return call_user_func($c, $a, $b) < 0;
        };

        self::_qsort($this->a, 0, count($this->a) - 1, $lt);
        return $this;
    }
    // @ref: http://www.php.net/manual/en/language.oop5.magic.php
    public function __toString() {
        return '[' .implode(',', $this->a).']';
    }

    public function map($func) {
        for ($i = 0, $n = count($this->a); $i < $n; $i++) {
            $this->a[$i] = call_user_func($func, $this->a[$i]);
        }
        return $this;
    }
}

// test#1
$a = [57, 68, 59, 52, 52, 72, 28, 96, 33, 24];
$ia = new MyArray($a);
$ia->qsort(function($a, $b) {
    return $a - $b;
});
echo $ia.PHP_EOL;  // [24,28,33,52,52,57,59,68,72,96]

// test#2
// "Hello world Blog Control" 字符串按空格分隔,分成数组,转化为小写,按字母顺序排
$s = "Hello world Blog Control Hola hi";

$sa = new MyArray(explode(' ', $s));
$sa->map(function($s) {
    return strtolower($s);
})->qsort(function($a, $b) {
    return strcmp($a, $b);
});
echo $sa.PHP_EOL; // [blog,control,hello,hi,hola,world]

  

快速排序算法  

  

猜你喜欢

转载自www.cnblogs.com/mingzhanghui/p/9274431.html