A quick sort of scala

Source: Internet
Author: User
Tags foreach array arrays empty sort

It's really more and more like Scala, concise grammar, fresh style is my impression of Scala, it is really very convenient to use Scala programming, from Scala design ideas can also get a lot of inspiration, such as the following a fast sort of array of numbers (array[ Int]) method, have you ever thought of implementing it in this way?

/**
  * 快速排序的例子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 <))
       )
     }
   }
}

The sort (Array[int] method completes the traditional quick sort function in a concise way:

1, to determine whether the parameter array is empty? If sorting is done for empty descriptions, direct method arguments.

2. If the given parameter number is not empty, get the middle number of the array.

3, divide the parameter array according to the middle number, and partition and generate a new array by calling the array filter (P:A => Boolean) method, "XS filter (Pivot >)" Generates a new array containing only numbers less than pivot, and "xs filter (pivot = =)" contains an array that is equal to pivot, and "XS Filter (Pivot <)" contains numbers greater than pivot, through the iteration of the Sort method, The sorting process was completed.

4, through the Array.concat method to merge multiple arrays, return the sort of result on the line.

The Sort method specifies the return value but there is no "returns" language in the method block, but it doesn't matter, and the Scala compiler can automatically judge.

This way and the traditional fast sorting method in time complexity and space complexity similar, but the code is greatly simplified, do not believe you use Java to write a fast array of digital sorting method (to write yourself, using Collections.sort (list<t>) method is not counted AH).

Scala has attracted a lot of attention, and some people have criticized Scala for comparing the shortcomings of Scala with the merits of Java, "while others have embraced Scala with Java flaws and Scala's virtues, then two teams have been fighting a spat on the forum, and Scala is not a banknote, Of course not to please everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.