第一次写快排的代码(可以研究下为什么运行的结果是这样的)
<span style="font-size:18px;">package data_structure;
import java.util.InputMismatchException;
import java.util.Scanner;
public class QuickSort {
private int array[];
public QuickSort(){
int i = 0, j, n;
Scanner scanner = new Scanner(System.in);
System.out.println("enter the length of array[]:");
n = scanner.nextInt();
array = new int[n];
System.out.println("enter the value of array[]:");
while (i < n) {
try {
j = scanner.nextInt();
} catch (InputMismatchException e) {
break;
}
array[i] = j;
i++;
}
for(int i1:array)
System.out.print(i1+" ");
System.out.println("");
}
public void quickSort(int left,int right){
if(left<right){ //递归终止条件,每个递归都必须有终止条件
int q=pagenation(left,right); //非随机化快排
//int q=randomPagenation(left,right); //随机化快排
quickSort(left,q-1);
quickSort(q+1,right);
}
}
/*非随机化快排*/
public int pagenation(int left,int right){
int i=left,j=right+1,q=left;
int x=array[q];
while(true){
while(array[++i]<x&&i<right);
while(array[--j]>x&&j>left);
if(i>=j){
break;
}
swap(i,j);
}
swap(q, j);
return j;
}
/*随机化快排*/
public int randomPagenation(int left,int right){
int r=(int)(Math.random()*(right-left+1)+left);
swap(r, left);
return pagenation(left,right);
}
public void swap(int i,int j){
array[i]=array[i]+array[j];
array[j]=array[i]-array[j];
array[i]=array[i]-array[j];
}
public static void main(String [] args){
QuickSort qSort=new QuickSort();
qSort.quickSort(0, qSort.array.length-1);
for(int i:qSort.array)
System.out.print(i+" ");
}
}
</span>
此时如果输入为 3 9 6 4 2 8 7 1 15 13 则结果为 1 2 3 0 0 7 8 0 13 15 (想想这是为什么???)
经过纠正后代码为
package data_structure;
import java.util.InputMismatchException;
import java.util.Scanner;
public class QuickSort {
private int array[];
public QuickSort(){
int i = 0, j, n;
Scanner scanner = new Scanner(System.in);
System.out.println("enter the length of array[]:");
n = scanner.nextInt();
array = new int[n];
System.out.println("enter the value of array[]:");
while (i < n) {
try {
j = scanner.nextInt();
} catch (InputMismatchException e) {
break;
}
array[i] = j;
i++;
}
for(int i1:array)
System.out.print(i1+" ");
System.out.println("");
}
public void quickSort(int left,int right){
if(left<right){ //递归终止条件,每个递归都必须有终止条件
int q=pagenation(left,right); //非随机化快排
//int q=randomPagenation(left,right); //随机化快排
quickSort(left,q-1);
quickSort(q+1,right);
}
}
/*非随机化快排*/
public int pagenation(int left,int right){
int i=left,j=right+1,q=left;
int x=array[q];
while(true){
while(array[++i]<x&&i<right);
while(array[--j]>x&&j>left);
if(i>=j){
break;
}
swap(i,j);
}
swap(q, j);
return j;
}
/*随机化快排*/
public int randomPagenation(int left,int right){
int r=(int)(Math.random()*(right-left+1)+left);
swap(r, left);
return pagenation(left,right);
}
/*这样交换变量没错,但是如果当i和j相等就完了,最后array[i]会是0,导致基准元素始终为0,得到错误结果*/
/*public void swap(int i,int j){
array[i]=array[i]+array[j];
array[j]=array[i]-array[j];
array[i]=array[i]-array[j];
}*/
public void swap(int i,int j){
int temp;
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
public static void main(String [] args){
QuickSort qSort=new QuickSort();
qSort.quickSort(0, qSort.array.length-1);
for(int i:qSort.array)
System.out.print(i+" ");
}
}
这样输入 3 9 6 4 2 8 7 1 15 13 则结果为 1 2 3 4 6 7 8 9 13 15 ,其中我对快排进行了优化,对基准值进行了随机化,这样就提高了算法的效率