Java Quick Sort

Source: Internet
Author: User

Quick sort Just remember two steps:

1. Locate a datum point, place the number greater than the datum point to the right of the Datum point, and the number less than the datum point to the left of the Datum point (can also be reversed, less than the right side, and greater than the left side)

2. The array obtained in step 1 is divided by the datum point to two parts (the left side is less than the number of datum points, the right side is greater than the number of datum points), respectively, the two parts recursively call step 1

The implementation code is as follows:

ImportJava.io.*; Public classQuickSort { Public Static voidMain (string[] args) {int[] arr = {9, 2, 2, 10, 3, 27, 1};//Array to sortQuickSort (arr, 0, Arr.length-1);  for(inti = 0; i < arr.length; i++) {System.out.println (Arr[i]+ "  "); }    }        /*arr: Array to sort left: Sort start position right: sort Stop position*/    Private Static voidQuickSort (int[] arr,intLeftintRight ) {        if(Left > Right | | arr = =NULL|| Arr.length = = 0) {            return; }        intPivot = Arr[left];//base on the value of the left boundary        inti =Left ; intj = right;//using local variables to receive parameters                 while(I! = j) {//When i = J indicates that it is sorted (that is, the datum point to the left is less than the datum point and the right side of the datum point is greater than the datum point)             while(Arr[j] >= pivot && i < J) {//find the number less than the datum point from right to leftj--; }             while(Arr[i] <= pivot && i < J) {//from left to right, find a number greater than the Datum pointi++; }            if(I < J) {//look for the above/below the reference point of the subscript has not met (met on the end of the round to sort out the loop), this time swap two positions                intTMP =Arr[i]; Arr[i]=Arr[j]; ARR[J]=tmp; }        }                //return the base number to the positionArr[left] =Arr[i]; Arr[i]=pivot; QuickSort (arr, left, I-1); QuickSort (arr, I+1, right); }}

Java Quick Sort

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.