A simple example of using Java to implement quick sorting

Source: Internet
Author: User

public static void Main (string[] args) {//test sort Random r = new random (); int arr[] = NE W int[10];for (int i=0;i<10;i++) {Arr[i] = r.nextint (100);} System.out.println ("before sort"); for (int i:arr) {System.out.print (i+ ",");} Fastsort (arr, 0, arr.length-1); System.out.println ("after Sort"), for (int i:arr) {System.out.print (i+ ",");}} public static void Fastsort (int[] arr, int left, int. right) {/** * I when looking from the right side of index * J when looking for index * TMP exchange data, temporary variable * baseval interchange Benchmark values For comparison */int I, J, TMP, Baseval;if (left > right) {return;} Baseval = Arr[left];i = Left;j = Right;while (i! = j) {//I and J are not the same, then do the following//to first right-to-left look while (Arr[j] >= baseval && I < J) {//until a value larger than baseval is found j--;} Then look left to right while (Arr[i] <= baseval && i < J) {//until a value smaller than baseval is found i++;} Swap the position of two numbers in the array if (I < j) {tmp = Arr[i];arr[i] = arr[j];arr[j] = tmp;}} Finally, the benchmark is arr[left] = arr[i];arr[i] = baseval;//recursion gets the left and right sort results fastsort (arr, leave, i-1); Fastsort (arr, i + 1, right);} 

  

A simple example of using Java to implement quick sorting

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.