Sort the Quick sort

Source: Internet
Author: User

package com.xsz.demo;/**    *  @author  cwqi   *  @date  2015-1-6   *///sort of the quick sort, the basic idea for the division + digging pits, there are two ways: one is double-sided scan, and the other is a single scan. public class quicksort {public static void main  (String[] args) {int  a[] ={2,3,30,1,4,56,2,7,3,8};//int a[] ={4,5,6,2,7,3,8};//int a[] ={1,5,4,3,8 };//int a[] ={1,1};//int a[] ={1};//int a[] ={};//int a[] =null;int  start=0;int end= a==null? -1:a.length -1;//singlescan (a,start,end);d Oublescan (A, Start,end);p rint (a,end);} Static void doublescan (int[] a, int start, int end)  { if (start  < end) {          int i = start,  j = end, key = a[start];           while (i < j) {          //from behind, looking for elements smaller than key values              while ( i < j && a[j] >=  Key) {                   j--;              }               if (I&NBSP;&LT;&NBSP;J) {                   a[i++] =  a[j];              }                            //from the front looking for elements larger than the key value              while (i &Lt; j && a[i] <= key) {                   i++;               }               if (I&NBSP;&LT;&NBSP;J) {                   a[j--] = a[i];               }          }                   a[i]  = key;  //i is the center of         doublescan (A, start, &NBSP;I-1);           doublescan (a, i+1, end);      &nbSP;}    }static void singlescan (int[] a, int s, int e)  {int  j = s+1;//int p = s;//Sub-location if  (s < e)  {int key  =a[s];//key value while  (j<=e) {if (A[j] <= key) {p++ ;//system.out.println ("key->" + key); Int temp =a[p];a[p] = a[j];a[j]=temp;//system.out.println ("Exchange----->" +a[j]+ " " +a[p]); j++ ;} else {j++ ;}} Place the key value in the sub-location Int temp =a[s];a[s] = a[p];a[p]=temp;singlescan (a, s, p-1); Singlescan (a, p+1, e);}} Static void print (int[]array ,int l) {for  (int i = 0; i <=  l; i++)  {system.out.print (array[i]+ " ");}}


Sort the Quick sort

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.