快排实现及其错误总结

       第一次写快排的代码(可以研究下为什么运行的结果是这样的)

   

<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  ,其中我对快排进行了优化,对基准值进行了随机化,这样就提高了算法的效率



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值