Classical Sorting algorithm Learning (Java implementation)

Source: Internet
Author: User

1. Algorithmic thinking

Split the sorted data into separate two parts, one of which is smaller than all the data in the other part,

Then the second method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, in order to achieve the entire data into an ordered sequence.

2. Algorithm diagram

  

3. Implementing the Code

 Static voidQuiksort (intN[],intLeftintRight ) {     intTemp//to define a temporary variable     if(Left <Right ) {Temp= Partition (n, left, right);//assigns the value returned by the partition to tempQuiksort (n, left, temp-1);//Recursive call continues the number of rows to the leftQuiksort (N, temp + 1, right);//Recursive call continues the number of rows to the right     } }   Static intPartitionintN[],intLeftintRight ) {     intPivot = N[left];//take the first number on the left as the base//Loop Right-to-left, pointer movement finds the first number less than pivot      while(Left < right && N[right] >=pivot) { Right--; }     if(Left < right)//after finding the first number less than pivotn[left++] = N[right];//fill this number in n[left] This position, left forward one block         /*The If section above is simplified as follows: if (left < right) {N[left] = N[right]; left++; Note: The use of the + + operator: To assign a value first and then to add it*/     //loop from left to right, the pointer moves to find the first number greater than pivot      while(Left < right && N[left] <=pivot) { Left++; }     if(Left < right)//find the first number greater than pivotn[right--] = N[left];//fill this number in n[right] This position, right forward a grid//simplify with the same reasoning    /*if (left < right) {N[right] = N[left]; right--; Note-The use of the operator: first assignment, then self-subtraction}*/N[left]= pivot;//when exiting, I equals J, the pivot is filled in, the number is divided into two parts, the number on the left is less than pivot, the number on the right is greater than pivot    returnLeft//returns the division position}

Classical Sorting algorithm Learning (Java implementation)

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.