The method of realizing quicksort fast sorting algorithm in Java by graphics and text _java

Source: Internet
Author: User

Relative bubble sorting, selection and other algorithms, the rapid sequencing of the specific principle and implementation of a certain degree of difficulty. In order to better understand the fast sorting, we still describe the algorithm principle of fast ordering in detail in the form of illustrative examples. In the previous sorting algorithm, we explained the height ranking problem of 5 athletes, in order to better reflect the characteristics of the fast sorting, we add 3 additional athletes here. Examples of 8 athletes and their height information are detailed below (F, G, h for new athletes): A (181), B (169), C (187), D (172), E (163), F (191), G (189), H (182)

In the previous sort algorithm, these sorts were done by coach-led, now the number of athletes increased, the coach also wanted to take the opportunity to rest, so the coach called two assistants, so that the two assistants to a quick sort of sorting method to achieve the height of 8 athletes from left to right, from low to high ranking.

According to the algorithm principle of the fast sorting method, two assistants stand on both sides of the athlete's arrangement respectively, as shown in the following picture:

First, the Assistant 1 selects an athlete from the arrangement (usually the 1th athlete on the left or the middle athlete), and the Player A (181) is selected here. Because sorting is from left to right, from low to high, so assistant 1 needs an athlete with a smaller height than a (181) (The selected a (181) as the benchmark for comparison, all of which are compared with the first selected athlete A (181)):

Now let's continue to refer to the detailed schematic of the first round of the quick sort.

When the two assistants meet in the sorting process, they stop the comparison of the current rounds and place the first selected athlete a (181) on the table where two assistants meet:

In a quick sort, when two assistants meet, the order of the round is over. At this point, we found that two athletes met position a (181) as a dividing point, ranked on the left are taller than a (181) of the athletes, ranked on the right side is taller than a (181) athletes. At this time, we then separate a (181) to the left and the right of the two sorting, if we continue to use the above two helper sorting methods on the two sides of the arrangement, then after many permutations, we will finally be able to get the sort results we need.

The above is the whole sort implementation process of fast sorting. The quick sort is to use the above collation, the arrangement is divided into two permutations, two arranged into four permutations, until no arrangement can be divided into stops, and finally get the sort results we need.

Now, we're still programming Java code to sort the height of the above 8 athletes using a quick sort:

/** * To quickly sort the elements in the specified array from start to end * * @param array specified by the array * @param start the array index start that needs to be sorted quickly * @param end of an array that needs to be sorted quickly  /public static final void QuickSort (int[] array, int start, int end) {//I is equivalent to assistant 1 position, J is equivalent to assistant 2 position int i = start, j =
  End int pivot = Array[i]; Take the 1th element as the datum element int emptyindex = i; The location index that represents the vacancy, the default is the position of the extracted datum element//If more than 1 elements need to be sorted, enter a quick sort (as long as I and J are different, it means that at least 2 of the array elements need to be sorted) while (I < j) {//Assistant 2 starts right to left one
    To find elements that are less than the datum element while (i < J && Pivot <= Array[j]) j--;
    if (I < J) {//If Assistant 2 finds the corresponding element before encountering the Assistant 1, the element is given to the Assistant 1 "vacancy", J becomes an empty array[emptyindex] = Array[emptyindex = j];
    }//Assistant 1 starts looking up from left to right an element that is greater than the datum element while (I < J && Array[i] <= pivot) i++;
    if (I < J) {//If Assistant 1 finds the corresponding element before encountering the Assistant 2, the element is given to the Assistant 2 "vacancy", I becomes an empty array[emptyindex] = Array[emptyindex = i];
  
  }//Assistant 1 and Assistant 2 will stop the loop after they meet, and the first removed datum to the final vacancy array[emptyindex] = pivot; ===== this round of quick sort completes =====//If there are more than 2 elements on the left side of the split point I, the recursive call continuesFast sort if (I-start > 1) {quickSort (array, 0, i-1);
  //If there are more than 2 elements on the right side of the split Point J, the recursive call continues to sort it quickly if (End-j > 1) {quickSort (array, j + 1, end); }//Main method public static void main (string[] args) {//===== Use the Quick Sort method to sort the array heights of 8 athletes ' height from low to high =====//A (181), B (169)
  , C (187), D (172), E (163), F (191), G (189), H () int[] Heights = {181, 169, 187, 172, 163, 191, 189, 182};
  Call Quick Sort Method QuickSort (heights, 0, heights.length-1);
  Output sorted result for (int height:heights) {System.out.println (height);

 }
}

The above Java code runs the results output as follows:

163
169
172
181
187
189
191

Note: Due to local thinking differences, there may be several variants of the code implementations mentioned above for quick sorting. However, regardless of the form of variation, the core idea of fast sequencing does not change.

Another implementation: a one-way scan
fast-sorted array segmentation There is also another one-way scan version, the specific step is to select the last element in the array as the Shard element, the same set two pointers, pointer I point to the first element in the array before a position, J point to the first element in the array. J scans from right to left, and when you encounter less than or equal to the Shard element, I plus one, then I exchange the elements that I and J point to. Finally, the elements of the i+1 position and the segmentation elements are exchanged to complete the array partitioning. The code implementation is as follows:

int partition (int[] A, int lo, int hi) {
  int i = lo-1, j = lo;
  int v = A[hi];
  while (J < hi) {
    if (A[j] <= v) {
      swap (A, ++i, j);
    }
    j + +;
  }
  Swap (A, i + 1, hi);
  return i + 1;
}

Related Article

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.