Sort by Fast Line

Source: Internet
Author: User

Code
public class QuickSort {public static void main (string[] args) {int[] a = {7, 2, 4, 5, 9, 6, ten, 3  , 0}; System.out.println (Arrays.tostring (a)); QuickSort (a); System.out.println (Arrays.tostring (a)); public static void QuickSort (int[] a) {if (a.length>0) {quickSort (A, 0, a.length-1);}} private static void QuickSort (int[] A, int low, int. high) {//1, find the exit if (Low > High) {return;} of the recursive algorithm; 2, save int i = Low;int j = High;//3,keyint key = a[low];//4, complete a trip sort while (i< j) {//4.1, from right to left find the first number greater than key while (I<j &A mp;& A[j] > key) {j--;} 4.2 From left to right find the first number less than key while (I<j && a[i] <= key) {i++;} 4.3 Interchange if (i<j) {int p = a[i];a[i] = a[j];a[j] = p;}} 4.4, adjust the position of key if (Key>a[i]) {int p = a[i];a[i] = A[low];a[low] = p;} 5, the number of keys to the left of the fast row quicksort (A, low, i-1),//6, to the right of the key to the number of fast row quicksort (A, i+1, high);}}
Quick-line principle

In the number of rows (such as array a) in the selection of a central value key (such as a[0]), through a sort of array a divided into two parts, with key as the center, key to the right of the key is larger than keys, key to the left of the key is small, and then repeat the process for the two parts, until the whole order
The whole process of the fast-running is simplified in order to go through the sequencing process, and then the recursive call is done.
A way to sort by:
1, define i=0,j=a.lenght-1,i as the first number of subscript, J for the last number subscript;
2, from the last number of the array AJ from right to left to find the first number less than key, recorded as AJ;
3, from the first number of arrays Ai from left to right, find the first number greater than key, recorded as AI;
4, exchanging AI and AJ;
5, repeat the process until i=j;
6, if A[i] is less than key, switch a[i] and key.

Suppose the array to be lined is: a[8] ={5 2 8 9 2 3 4 9}

Select key = 5, start i=0,j=7

Index 0 1 2 3 4 5 6 7

Start: 5 2 8 9 2 3 4 9

I J

First time looking for 5 2 8 9 2 3 4 9

I J

Exchange: 5 2 4 9 2 3 8 9

I J

Second time look 5 2 4 9 2 3 8 9

I J

Exchange: 5 2 4 3 2 9 8 9

I J

Third time look 5 2 4 3 2 9 8 9

I J

Adjustment Key:2 5 4 3 5 9 8 9

I J

Sort by Fast Line

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.