Little Orange Book Reading Guide (vi)--quick sorting and three-direction segmentation quick Sorting

Source: Internet
Author: User
Tags sorts

Algorithm Description: Fast sorting is a sort algorithm of divide and conquer. It divides the array into two sub-arrays and arranges the two parts independently. Quick sort and Merge sort are complementary: merge sort sorts the array into two sub-arrays, merges the sub-arrays to sort the entire array, and the quick sort sorts the array in the same way that when two sub-arrays are ordered, the entire array is naturally ordered.

Algorithm diagram:

Algorithm interpretation: Select the underlying element (5) and the convenient array, the element that has less than 5 is arranged on its left, and the elements greater than 5 are arranged on the right side of it. Then the left sub-array and the right sub-array are processed by recursive method respectively.

The difficulty with fast sorting is to try not to use extra storage space (that is, to ensure in-place segmentation) and how to handle elements that are equal to the underlying element (5).

Java code example:

 Packagealgorithms.sorting;Importalgorithms. sortable;Importalgorithms.common.Arrays;ImportAlgorithms.common.ArraysGenerator;/*** Created by Learnhow on 2018/8/17.*/ Public classQuickextendsArrays<integer>ImplementsSortable<integer>{@Override Public voidsort (integer[] array) {sort (array,0, Array.length-1); }    //Recursive body    Private voidSort (integer[] array,intLointhi) {        if(Lo >=hi) {            return; }        intPart =partition (Array, lo, HI); Sort (Array, lo, part-1); Sort (Array, part+ 1, HI); }    //Segmentation Algorithm    Private intPartition (integer[] array,intLointhi) {        //Bounds array traversal range Array (lo, hi];        inti =Lo; intj = Hi + 1; inttemp =Array[lo];  while(true) {             while(Array[++i] <temp) {                if(i = =hi) {                     Break; }            }             while(Array[--j] >temp) {                if(J = =lo) {                     Break; }            }            if(I >=j) { Break;        } Exchange (Array, I, j);        } exchange (Array, lo, j); returnJ; }     Public Static voidMain (string[] args) {integer[] arr= Arraysgenerator.generate (10, 0, 10); Quick Quick=NewQuick ();        Quick.sort (arr);    System.out.println (java.util.Arrays.toString (arr)); }}

Qt/c++ code Example:

voidQuick::sort (int*arr,intLointhi) {    if(Lo >=hi) {        return; }    intPart =partition (arr, lo, HI); Sort (arr, lo, part-1); Sort (arr, part+1, HI);}intQuick::p artition (int*arr,intLointhi) {    inti =Lo; intj = Hi +1; intTargetvalue =Arr[lo];  while(true) {         while(Arr[++i] <targetvalue) {            if(i = =hi) {                 Break; }        }         while(Arr[--j] >targetvalue) {            if(J = =lo) {                 Break; }        }        if(I >=j) { Break; }        //Swap element Location        inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }    inttemp =Arr[lo]; Arr[lo]=Arr[j]; ARR[J]=temp; returnJ;}

Algorithm Performance Analysis:

Quick Sort Speed advantage the number of comparisons with it is small, but the sort effect depends on the case of the tangent fraction group, and if there are a large number of repeating elements in the array. Algorithm performance can also be significantly improved. Below we give a fast sorting algorithm for three-direction segmentation. As you can understand.

Algorithm diagram:

Algorithm explanation: In the three-direction segmentation fast sorting algorithm We define three pointer variables: the upper bound of the GT is less than V, the upper bound of the array I equals V, and the lower bound of the GT greater than the V array. and an intermediate array of array[i, GT]. The process of judging a single traversal is as follows:

    • Array[i] Less than V, the array[lt] and Array[i] exchange, and the LT and I respectively plus 1;
    • Array[i] Greater than V, will ARRAY[GT] and Array[i] exchange, and the GT minus 1;
    • Array[i] is equal to V, and I add 1 separately;

Java code example:

Package Algorithms.sorting;import algorithms. Sortable;import Algorithms.common.arrays;import Algorithms.common.ArraysGenerator; Public classQuick3way extends arrays<integer> implements Sortable<integer>{@Override Public voidsort (integer[] array) {sort (array,0, Array.Length-1); }    Private voidSort (integer[] array,intLointhi) {        if(Hi <=lo) {            return; }        inttemp = Array[lo];//Compare the underlying        intlt = lo;//index record less than temp        intEQ = lo +1;//index record equal to temp        intGT = Hi;//index record greater than temp         while(EQ <=GT) {            if(Array[eq] <temp) {Exchange (array, lt+ +, eq++); } Else if(Array[eq] >temp) {Exchange (array, EQ, GT--); } Else{eq++; }} sort (array, lo, lt-1); Sort (array, GT+1, HI); }     Public Static voidMain (string[] args) {integer[] arr= Arraysgenerator.generate (Ten,0,Ten); Quick3way Quick3way=NewQuick3way ();        Quick3way.sort (arr); System. out. println (java.util.Arrays.toString (arr)); }}

Qt/c++ code example (slightly)

Three-way segmentation quick sorting can be very good for a particular array, but it's often easy to get a wrong deal with what's going on in the subscript. Therefore, while considering the performance of the algorithm, it should be given priority to ensure correctness.

RELATED links:

Algorithms for Java

Algorithms for Qt

Little Orange Book Reading Guide (vi)--quick sorting and three-direction segmentation quick Sorting

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.