Analysis and implementation of Java sorting algorithm: Fast row, bubble sort, select sort, insert sort, merge sort (ii)

Source: Internet
Author: User
Tags array length

First, overview:

  The previous blog describes common simple algorithms: bubble sort, select sort, and insert sort. This article describes the Advanced sorting algorithm: Quick sort and merge sort. Before we begin to introduce the algorithm, we first introduce the basic knowledge required by advanced algorithms: partitioning, recursion, and incidentally introducing the binary lookup algorithm.

Second, Division:

  Partitioning is the precondition for fast ordering, which divides data into two groups, with data greater than a specific value in one group, and less than a specific value in another group. Fast sorting is done by partitioning and recursive operations.

(1) Principle:

Define a threshold, respectively, from the leftmost and rightmost traversal elements, the left to find a greater than the threshold value of the data will stop, the right to find a less than the threshold value of the data will stop, if both sides have not reached the middle, then the left is greater than the threshold value of the data and the right less than the threshold value of data; Until the left hand and the right hand pointer meet, at this time the left data are less than the threshold value, the right data are greater than the threshold, the end of the division. At the end of the divide, the data is still unordered, but closer to the order.

(2) Example:

Data to be divided: 7, 6, 9, 8, 5, 1, assuming a threshold value of 5

The first round: the left hand pointer to 7, the right pointer to 1, the left pointer to move backward, the right pointer to the left, found that the first element on the left is greater than 5 7, the first of the second less than 5 element 1, the position of the Exchange 7 and 1, the result: 1,6,9,8,5,7;

Second round: Starting from 6 to find a number greater than 5, find 6, right from 5 to find less than 5 number, find 1, but at this time because 6 on the right side of 1, that is, the left hand pointer < The pointer, the pointer crosses around, at this time the division ends. The original sequence is divided into two parts, the left sub-series has only one element, that is, 1, which is less than the threshold of the sub-series; The right sub-column consists of 5 elements, all of which are greater than the threshold value 5.

(3) Code implementation:

 PackageCom.test.insertsort;/*** Division, recursion, fast line *@authorBJH **/ Public classQuickSort {/**To sort, divide an array*/    Private int[] array; /**Array Length*/    Private intlength;  PublicQuickSort (int[] Array) {         This. Array =Array;  This. length =Array.Length; }        /*** Print Elements*/     Public voidPrintArray () { for(inti=0; i<length; i++) {System.out.print (Array[i]+" ");    } System.out.println (); }            /*** Division *@returndividing point of demarcation*/     Public intPartitionintLeftintRightintpivot) {        //The starting point of the left pointer, Left-1 is because in the back loop, the left pointer is shifted to the right at once per loop ,//This ensures that the left pointer starts from the first element on the left, or it starts with the second one.        intLeftpoint = left-1; //The starting point of the right pointer, right+1 is due to the back of the loop, each loop once the right pointer is shifted to the left,//This ensures that the right pointer starts from the far right, or else it starts at the bottom of the second.        intRightPoint = right+1;  while(true){            //find data on the left that is larger than pivot, or go to the far right and still not find data larger than pivot             while(Leftpoint<right && array[++leftpoint]<pivot); //find data on the right less than pivot, or go to the far left and still not find a smaller data than pivot             while(Rightpoint>left && array[--rightpoint]>pivot); //left and right pointers overlap or intersect            if(Leftpoint >=rightpoint) {                 Break; }Else{                //swap big and right small data on the leftswap (Leftpoint,rightpoint); }        }        //returns the cutoff point, which is the leftmost point in the right sub-array        returnLeftpoint; }            /*** Exchange Data*/     Public voidSwapintLeftpoint,intrightpoint) {        inttemp =Array[leftpoint]; Array[leftpoint]=Array[rightpoint]; Array[rightpoint]=temp; }         Public Static voidMain (String args[]) {int[] Array = {99,78,26,17,82,36,9,81,22,100,30,20,17,85}; QuickSort QS=NewQuickSort (array); System.out.println ("The data before the partition is:");        Qs.printarray (); intbound = qs.partition (0, Array.length-1, 50); System.out.println ("The Divided data is:");        Qs.printarray (); System.out.println (The dividing point is: "+ Array[bound] +", the coordinates of the dividing point are: "+bound); }}

The result of the operation is:

Ongoing updates ....

Analysis and implementation of the Java sorting algorithm: Fast row, bubble sort, select sort, insert sort, merge sort (b)

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.