- /**
- * 快速排序的例子2
- * @author VWPOLO
- * < p>2009-8-12< /p>
- */
- object TestQuickSort2 {
- def main(args : Array[String]) : Unit = {
- var arrays = Array(123,343,5435,23,3213);
- Console.println("排序前的结果");
- arrays.foreach((array: Int ) => println(array))
- arrays = sort(arrays);
- Console.println("排序后的结果");
- arrays.foreach((array: Int ) => println(array))
- }
-
- def sort(xs: Array[Int]):Array[Int] = {
- if(xs.length < = 1)
- xs;
- else {
- val pivot = xs(xs.length /2);
- Array.concat(
- sort(xs filter (pivot >)),
- xs filter (pivot ==),
- sort(xs filter (pivot < ))
- )
- }
- }
- }
|