选择排序(解析及代码实现)


package cn.hncu.sort;

public class SelectSort {

	//记录当前最小的数下标,与第一个交换
	public void selectSort(double[] a,int type){
		//排序n-1即可,每次排序决定a[i]
		for (int i = 0; i < a.length-1; i++) {
			int k=i;//记录当前最小数下标
			for (int j = i+1; j < a.length; j++) {
				if (type==0) {//从小到大排
					if (a[j] < a[k])
						k = j;//发现更小数,更新k
				}else//从大到小排
					if (a[j] > a[k])
						k = j;//发现更大数,更新k
			}
			if(k!=i)//发现后面有更小数/更大数时 交换至a[i]
				swap(a, i, k);
		}
	}
	//默认从小到大排
	public void selectSort(double[] a){
		selectSort(a,0);
	}
	//对某一段排序
	public boolean selectSort(double[] a,int first,int end,int type){
		if(first<=0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		System.arraycopy(a, first, b, 0, b.length);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(double[] a,int first,int end){
		return selectSort(a, first, end, 0);
	}
	//float[]数组
	public void selectSort(float[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(float)b[i];
		}
	}
	public void selectSort(float[] a){
		selectSort(a, 0);
	}
	public boolean selectSort(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);//截取
		selectSort(b, type);//排序
		for (int i = 0; i < b.length; i++) {//放回
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(float[] a,int first,int end) {
		return selectSort(a, first, end, 0);
	}
	//long[]数组
	public void selectSort(long[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(long)b[i];
		}
	}
	public void selectSort(long[] a){
		selectSort(a, 0);
	}
	public boolean selectSort(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);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(long[] a,int first,int end) {
		return selectSort(a, first, end, 0);
	}
	//int[]数组
	public void selectSort(int[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(int)b[i];
		}
	}
	public void selectSort(int[] a){
		selectSort(a, 0);
	}
	public boolean selectSort(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);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(int[] a,int first,int end) {
		return selectSort(a, first, end, 0);
	}
	//short[]数组
	public void selectSort(short[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(short)b[i];
		}
	}
	public void selectSort(short[] a){
		selectSort(a, 0);
	}
	public boolean selectSort(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);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(short[] a,int first,int end) {
		return selectSort(a, first, end, 0);
	}
	//byte[]数组
	public void selectSort(byte[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(byte)b[i];
		}
	}
	public void selectSort(byte[] a){
		selectSort(a, 0);
	}
	public boolean selectSort(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);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(byte[] a,int first,int end) {
		return selectSort(a, first, end, 0);
	}
	//char[]数组
	public void selectSort(char[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(char)b[i];
		}
	}
	public void selectSort(char[] a){
		selectSort(a, 0);
	}
	public boolean selectSort(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);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(char[] a,int first,int end) {
		return selectSort(a, first, end, 0);
	}
	//String[]数组
	public void selectSort(String[] a,int type){
		for (int i = 0; i < a.length-1; i++) {
			int k=i;//记录当前最小数下标
			for (int j = i+1; j < a.length; j++) {
				if (type==0) {//从小到大排
					if (a[j].compareTo(a[k])<0)
						k = j;//发现更小数,更新k
				}else//从大到小排
					if (a[j].compareTo(a[k])>0)
						k = j;//发现更大数,更新k
			}
			if(k!=i)//发现后面有更小数/更大数时 交换至a[i]
				swap(a, i, k);
		}
	}
	//默认从小到大排
	public void selectSort(String[] a){
		selectSort(a, 0);
	}
	//对某一段排序
	public boolean selectSort(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);
		selectSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean selectSort(String[] a,int first,int end) {
		return selectSort(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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值