Data structure-fast sorting algorithm

Source: Internet
Author: User

A quick sort of algorithm is:

1) Set two variables I, J, at the beginning of the order : i=0,j=n-1;

2) The first array element as the key data, assigned to the key, that is key =a[0];

3) Forward search from J, that is, after starting the forward search (j--), find the first value less than key A[j], will a[j] and A[i] interchange;

4) Backward search from I, that is, start backward search (i++), find the first a[i] greater than key ], interchange a[i] and A[J];

5) Repeat 3rd, 4, until i=j, (3,4 step, did not find the matching criteria, that is, 3 a[j] is not less than key, 4 A[i] is not larger than the time of the key change J, I value, so j=j-1,i=i+1, until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).

(1Basic idea: Select a datum element,usually select the first element or the last element,split the pending sequence into two parts through a scan,part smaller than the base element,a part is greater than or equal to the datum element,At this point the datum element is in the correct position after it is sorted,then the same method is used recursively to sort the two parts of the division.

(2) The implementation code is as follows:

public class quicksort{ /* *  Quick Sort  */int a[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};   public  quicksort () {      quick (a);       for (int  i=0;i<a.length;i++) {         system.out.print (a[i]+ ")   ");     }}        public int  Getmiddle (Int[] list, int low, int high)  {    int tmp  = list[low];    //the first axis of the array as a           while  (Low < high)  {         while   (LOW&NBSP;&LT;&NBSP;HIGH&NBSP;&AMP;&AMP;&NBSP;LIST[HIGH]&NBSP;&GT;=&NBSP;TMP)  {          high--;          }         list[low] =  list[high];   //smaller than mid-axis records move to low-end          while   (LOW&NBSP;&LT;&NBSP;HIGH&NBSP;&AMP;&AMP;&NBSP;LIST[LOW]&NBSP;&LT;=&NBSP;TMP)  {          low++;         }      list[high] = list[low];   //is larger than the mid-axis record moved to high-end            }         list[low] =  tmp;              //axis record to tail           return low;                    //return to the location of the axis   }     public void _qUicksort (Int[] list, int low, int high)  {     if  ( Low < high)  {     int middle = getmiddle (list,  Low, high)   //the list array into a split      _quicksort (list, low,  middle - 1)         //recursive ordering of low-word tables         _quicksort (List, middle + 1, high);        //recursive ordering of high-character tables      }}   public void quick (INT[]&NBSP;A2)  {if  (a2.length>0)  {    //to see if the array is empty      _ QuickSort (a2,0,a2.length-1);}} Public static void main (String[] args)  {quicksort qs=new quicksort (); qs._ QuickSort (qs.a,0,qs.a.length-1);}}


Data structure-fast sorting algorithm

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.