The principle of fast sorting and the realization of Java code _java

Source: Internet
Author: User

Overview

Fast sequencing is a sort algorithm developed by Donny Holl. In average, the sort n items are 0 (NLOGN) times compared. In fact, fast sorting is typically faster than the other 0 (NLOGN) algorithms, because its internal loop (inner Loop) can be implemented very efficiently on most architectures, and in most real-world data, it is possible to determine the choice of design and reduce the likelihood of two of times.

A quick sort, which divides the backlog into two separate parts by a single pass, in which some of the records have less keywords than the other, and then the two parts are sorted separately to achieve the order of the entire sequence.

Image diagram:

Steps

Select a datum element, usually the first element or the last element
A trip sort divides the records to be sorted into two separate parts, with some of the recorded element values smaller than the datum element values. The other part of the record has an element value that is larger than the base value
The correct position of the datum element at the time it is sorted
The two-part records are then continued to be sorted in the same way until the entire sequence is ordered.

Instance

Raw data:

3 5 2 6 2
Select 3 as the benchmark

First round

Look from right to left, smaller than 3, 2 in accordance with, will 2 and 3 swap 2 5 2 6 3
swap once, look in the direction of the reverse, from left to right to find than 3, 5 in line with, swap 2 3 2 6 5
again from the right to the
left to find more than 3 small, 2 compliant, swap
2 2 3 6 5
   
     > round one end

   

Second round

for [2 2] in the same way as above, get
2 2 3 6 5

Third round

for [6 5] in the same way as above, get
2 2 3 5 6

Final results

2 2 3 5 6

Code implementation (Java)

Package com.coder4j.main.arithmetic.sorting;

 public class Quick {private static int mark = 0; 
  /** * Auxiliary Exchange Method * * @param array * @param A * @param b/private static void Swap (int[] array, int a, int b) {
   if (a!= b) {int temp = Array[a];
   Array[a] = array[b];
   ARRAY[B] = temp;
   Find the match, swap System.out.println ("swap" + Array[a] + "and" + array[b] + ", get");
   for (int i:array) {System.out.print (i + "");
  } System.out.println (); /** * NEW round * * @param array * @param low * @param high * @return/private static int partition (I
  NT array[], int low, int high) {int base = Array[low];
  mark++;
  System.out.println ("in progress" + Mark + "wheel separated, Area:" + Low + "-" + high);
    while (Low < high) {while (Low < high && Array[high] >= base) {high--;
   System.out.println ("From right to left" than "+ base +" Small, pointer change: "+ Low +"-"+ high);
   Swap (array, low, high);
 while (Low < high && Array[low] <= base) {   low++;
   System.out.println ("Look from left to right" + base + "big, pointer change:" + Low + "-" + high);
  Swap (array, low, high);
 return to Low; /** * Fast sort of array, recursive call * * @param array * @param low * @param heigh * @return/private static int[] Qu
   Icksort (int[] array, int low, int heigh) {if (% < heigh) {int division = partition (array, low, heigh);
   QuickSort (array, low, division-1);
  QuickSort (Array, Division + 1, Heigh);
 } return array; /** * Quick Sort * @param array * @return/public static int[] Sort (int[] array) {return quickSort array,
 0, Array.length-1);
  public static void Main (string[] args) {int[] array = {3, 5, 2, 6, 2};
  int[] sorted = sort (array);
  SYSTEM.OUT.PRINTLN ("final result");
  for (int i:sorted) {System.out.print (i + "");

 }
 }

}

Test output results:

Full-selection copy into the notes are being separated by the 1th round, area: 0-4
swap 2 with 3, get
2 5 2 6 3 
from left to right looking than 3 large, pointer change: 1-4
swap 3 with 5, get 2 3 2 6, 5 
from right to left to find than 3 small, Pointer change: 1-3
from right to left than 3 small, pointer change: 1-2
swap 2 with 3, get 2 2 3 6, 5 
from left to right, pointer change: 2-2
is in the 2nd round of separation, area: 0-1
Look from right to left than 2 small, pointer change: 0-0
is carrying out the 3rd round of separation, the region: 3-4
swap 5 and 6, get 2 2 3 5, 
look from left to right to find than 6 large, pointer change: 4-4
end result
6 2 2 3 5 

Tested, consistent with the results in the example.

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.