package com.array.sort;
public class QuickSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] = {12,43,11,5,23,76,99,56,22};
QuickSort a = new QuickSort();
a.printss(arr);
a.quicksort(arr, 0, arr.length-1);
System.out.println("\n after:");
a.printss(arr);
}
public void printss(int arr[]){
for(int i = 0;i< arr.length;i++){
System.out.print(arr[i] + ",");
}
}
public void quicksort(int arr[],int first,int end){
if(first < end){
int pt = ptr(arr, first, end);//问题分解,pt是轴值在序列中的位置
quicksort(arr,first,pt - 1);//递归的对左侧序列进行快序排序
quicksort(arr,pt + 1,end);//递归的对右侧序列进行排序
}}
public int ptr(int arr[],int first,int end){
int i = first;
int j = end;
while( i < j ){
while(i < j && arr[i] <= arr[j])
j--;
if(i < j)
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
}
while(i < j && arr[i] <= arr[j])
i++;
if(i < j)
{
int temp = arr[j];
arr[j]= arr[i];
arr[i] = temp;
j--;
}
}
return i;
}
}
public class QuickSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] = {12,43,11,5,23,76,99,56,22};
QuickSort a = new QuickSort();
a.printss(arr);
a.quicksort(arr, 0, arr.length-1);
System.out.println("\n after:");
a.printss(arr);
}
public void printss(int arr[]){
for(int i = 0;i< arr.length;i++){
System.out.print(arr[i] + ",");
}
}
public void quicksort(int arr[],int first,int end){
if(first < end){
int pt = ptr(arr, first, end);//问题分解,pt是轴值在序列中的位置
quicksort(arr,first,pt - 1);//递归的对左侧序列进行快序排序
quicksort(arr,pt + 1,end);//递归的对右侧序列进行排序
}}
public int ptr(int arr[],int first,int end){
int i = first;
int j = end;
while( i < j ){
while(i < j && arr[i] <= arr[j])
j--;
if(i < j)
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
}
while(i < j && arr[i] <= arr[j])
i++;
if(i < j)
{
int temp = arr[j];
arr[j]= arr[i];
arr[i] = temp;
j--;
}
}
return i;
}
}