冒泡排序(解析及代码实现)

冒泡排序(解析及代码实现)_第1张图片

冒泡排序(解析及代码实现)_第2张图片

package cn.hncu.sort;


public class BubbleSort {
	
	//一般的冒泡排序(从小到大,最大的冒到数组尾部)
	public void eliminatebubbleSort(double[] a){
		//a.length-1就可完成排序,每一次排出最大的放尾
		for (int i = 1; i < a.length; i++) {
			//每一次比较后,尾部最大,无序比较,前0-a.length-i位排序
			for (int j = 0; j < a.length-i; j++) {
				if(a[j]>a[j+1])
					swap(a,j,j+1);//交换
			}
		}
	}
	//优化后冒泡,当一次遍历为发现交换式,后面无需再比
	public void bubbleSort(double[] a,int type){
		for (int i = 1; i < a.length; i++) {
			boolean isSwap=false;
			for (int j = 0; j < a.length-i; j++) {
				//从小到大排
				if(type==0){
					if(a[j]>a[j+1]){
						swap(a, j, j+1);
						isSwap=true;
					}
				}else{//从大到小排
					if(a[j]=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	//默认某一段从小到大
	public boolean bubbleSort(double[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	
	//float[]数组
	public void bubbleSort(float[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(float)b[i];
		}
	}
	public void bubbleSort(float[] a){
		bubbleSort(a, 0);
	}
	public boolean bubbleSort(float[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		float[] b=new float[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);//截取
		bubbleSort(b, type);//排序
		for (int i = 0; i < b.length; i++) {//放回
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(float[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//long[]数组
	public void bubbleSort(long[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(long)b[i];
		}
	}
	public void bubbleSort(long[] a){
		bubbleSort(a, 0);
	}
	public boolean bubbleSort(long[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		long[] b=new long[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(long[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//int[]数组
	public void bubbleSort(int[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(int)b[i];
		}
	}
	public void bubbleSort(int[] a){
		bubbleSort(a, 0);
	}
	public boolean bubbleSort(int[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		int[] b=new int[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(int[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//short[]数组
	public void bubbleSort(short[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(short)b[i];
		}
	}
	public void bubbleSort(short[] a){
		bubbleSort(a, 0);
	}
	public boolean bubbleSort(short[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		short[] b=new short[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(short[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//byte[]数组
	public void bubbleSort(byte[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(byte)b[i];
		}
	}
	public void bubbleSort(byte[] a){
		bubbleSort(a, 0);
	}
	public boolean bubbleSort(byte[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		byte[] b=new byte[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(byte[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//char[]数组
	public void bubbleSort(char[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(char)b[i];
		}
	}
	public void bubbleSort(char[] a){
		bubbleSort(a, 0);
	}
	public boolean bubbleSort(char[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		char[] b=new char[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(char[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//String[]数组
	public void bubbleSort(String[] a,int type){
		for (int i = 1; i < a.length; i++) {
			boolean isSwap=false;
			for (int j = 0; j < a.length-i; j++) {
				//从小到大排
				if(type==0){
					if(a[j].compareTo(a[j+1])>0){
						swap(a, j, j+1);
						isSwap=true;
					}
				}else{//从大到小排
					if(a[j].compareTo(a[j+1])<0){
						swap(a, j, j+1);
						isSwap=true;
					}
				}
			}
			if(!isSwap)
				break;
		}
	}
	//默认从小到大排
	public void bubbleSort(String[] a){
		bubbleSort(a, 0);
	}
	//对某一段排序
	public boolean bubbleSort(String[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		String[] b=new String[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		bubbleSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean bubbleSort(String[] a,int first,int end) {
		return bubbleSort(a, first, end, 0);
	}
	//交换
	private void swap(double[] a, int j, int i) {
		//借助第三变量
		double temp=a[i];
		a[i]=a[j];
		a[j]=temp;
		//不借助第三变量
		//1
//		a[i]=a[i]+a[j];
//		a[j]=a[i]-a[j];
//		a[i]=a[i]-a[j];
		//2
//		a[i]=a[i]^a[j];
//		a[j]=a[i]^a[j];
//		a[i]=a[i]^a[j];
		
	}
	private void swap(String[] a, int j, int i) {
		String temp=a[i];
		a[i]=a[j];
		a[j]=temp;
	}
	//输出
	private static void print(String[] a) {
		for (String x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(double[] a) {
		for (double x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(float[] a) {
		for (float x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(long[] a) {
		for (long x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(int[] a) {
		for (int x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(short[] a) {
		for (short x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(byte[] a) {
		for (byte x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(char[] a) {
		for (char x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
}
冒泡排序(解析及代码实现)_第3张图片

你可能感兴趣的:(常用的几种排序方法)