Interval partitioning method and Java implementation example in fast sorting algorithm for detailed _java

Source: Internet
Author: User

The fast sort is a sort of division Exchange C.r.a.hoare in 1962. The basic idea of the method is:
1. First, take a number out of the series as the base number.
2. The partitioning process, which places the number larger than this to its right, is less than or equal to its number on the left.
3. Repeat the second step to the left and right intervals until the interval is only one number.
The idea of the algorithm is very clear, but if the boundary value is not handled well in the interval dividing process, it is also very easy to appear the bug. The following two kinds of clearer thinking to guide the compilation of the interval Division code.
The first kind of thinking is the so-called pit-digging method of thinking, the following analysis of an example to analyze the process of digging the pit:
Taking an array as an example, the first number of intervals is the base number.

At initial time, left = 0;   Right= 9; X = A[left] = 72
Since the number in a[0 has been saved to X, it can be understood that a hole has been dug in the array a[0] and other data can be populated here.
Start from right and look forward to a <=x number. Obviously, when right=8, meet the conditions, will a[8] dug and then fill in the last pit A[left]. Such a pit a[0] was taken care of, but formed a new pit a[8]. Simple, then find the number to fill a[8] this pit. This time from left start to find a number greater than X, when left=3, meet the conditions, will a[3] dug and then fill in the last pit a[right];
Array into:


To repeat the above steps, the final array will become the following form:

You can see that the number in front of a[5] is smaller than it, and the number after a[5] is greater than it. Insert x into the pit of a[5] and the data becomes:

So again to a[0 ... 4] and a[6 ... 9] These two sub ranges repeat the above steps.
Summarize the number of digging pits
1. I =l; j = R; Dig up the base number to form the first pit a[i].
2. j--from behind to find the number of smaller than it, found after digging this number to fill a pit a[i].
3. i++ from the front to find a larger number than it, found also dug this number to fill in the previous pit a[j].
4. Repeat 2, 32 steps until I==j, and fill in a[i].
Follow this partitioning method to quickly sort Java code as follows:

public class Partition {/** * based on base division, small on the left, large on the right, does not require the entire sequence of sequential * * @param ary * @param base/static 
  void sort (int[] ary, int base) {int left = 0; 
   
  int right = Ary.length-1; 
   
  int leftpoint = left, rightpoint = right; while (true) {//split between the left and right sides while the right and left, while (Leftpoint < right && ary[leftpoint++] < base); Leftpoint is greater than right or ary[leftpoint]>base stop loop while (RightPoint >= left && ary[rightpoint--] > base ); 
   Conversely System.out.println ("the index to the left to be exchanged:" + (leftpoint-1)); 
   SYSTEM.OUT.PRINTLN ("The index on the right needs to be exchanged:" + (rightpoint+1));  There are two indexes that do not meet the criteria, that is, two indexes that need to be exchanged (Leftpoint-1 < RightPoint + 1) {//Need swap swap (ary, leftpoint-1, RightPoint + 
    1); 
    Util.printarray (ary); 
    Leftpoint = left; 
   RightPoint = right; 
   } else {break; 
  }} private static void (int[] ary, int a, int b) {int temp = Ary[a]; 
  Ary[a] = ary[b]; 
 ARY[B] = temp; } public Static void Main (string[] args) {int[] ary = Util.generateintarray (10); 
  System.out.println ("Original sequence:"); 
  Util.printarray (ary); 
  Sort (ary, 5); 
  System.out.println ("after sort:"); 
 Util.printarray (ary); 
 } 
}

Results:

Original sequence: 
[2, 8, 4, 3, 7, 5, 1, 9, 0, 6] The index to the left that needs to be exchanged: 1 The index to the right that needs to be exchanged: 8 [2, 0, 4, 3, 7, 5, 1, 9, 8, 6 
] 
the index on the left to exchange: 4 
the index on the right that needs to be exchanged: 6 
[2, 0, 4, 3, 1, 5, 7, 9, 8, 6] 
Index to the left to Exchange: 5 The 
index to the right needs to be exchanged: 5 
after sort: 
[2, 0, 4, 3, 1, 5, 7, 9, 8, 6] 

Another guiding thought of interval Division:
Divides the first element of the array as an interval, starting with the second element until the result is as shown in the figure,

The right boundary value and T of the l<t interval are then exchanged to form the following results:

So, you can write the quick sort code as follows:

public void Qsort (int. array[],int Left,int right) 
 { 
  if (left < right) { 
 
   int key = Array[left]; 
 
   int high = right; 
    
   int low = left+1; 
    
   while (true) {while 
     
    (low <= && Array[low] <= key) low++; 
 
    while (Low <= && Array[high] >= key) high--; 
        
    if (Low > High) break 
     ; 
 
    Swap (Array,low,high); 
   } 
   Swap (Array,left,high); 
    
   PrintArray (array); 
 
   Qsort (array,left,high-1); 
 
   Qsort (array,high+1,right); 
  } 
  

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.