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;<&NBSP;J) { a[i++] = a[j]; } //from the front looking for elements larger than the key value while (i ≪ j && a[i] <= key) { i++; } if (I&NBSP;<&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