插入排序(解析及代码实现 二分优化)

public void eliminateinsertSort2(int[] a,int type){
		for (int i = 0; i < a.length-1; i++) {
			int temp=a[i+1];
			int j=i;
			if (type==0) {//从小到大
				while (a[j] > temp) {//循环,找到位置
					a[j+1]=a[j];//大数后移
					j--;
					if(j<0)
						break;
				}
				a[j+1]=temp;//插入
			}else{//从大到小
				while (a[j] < temp) {
					a[j+1]=a[j];//小数后移
					j--;
					if(j<0)
						break;
				}
				a[j+1]=temp;
			}
		}
	}

//利用二分搜索优化插排
	
	//插排 前面是有序序列,从后往前一个个比较太慢
	//对于大量数据来说,慢,采用二分,快速定位位置
	public void insertSort(double[] a,int type){
<span style="white-space:pre">		</span>for (int i = 0; i < a.length-1; i++) {
<span style="white-space:pre">			</span>double temp=a[i+1];
<span style="white-space:pre">			</span>int left=0;
<span style="white-space:pre">			</span>int right=i;
<span style="white-space:pre">			</span>while(left<=right){//定位
<span style="white-space:pre">				</span>int mid=(left+right)/2;
<span style="white-space:pre">				</span>if(temp>a[mid]){
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>left=mid+1;
<span style="white-space:pre">					</span>}else{
<span style="white-space:pre">						</span>right=mid-1;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}else{
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>right=mid-1;
<span style="white-space:pre">					</span>}else{
<span style="white-space:pre">						</span>left=mid+1;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>//移动
<span style="white-space:pre">			</span>for (int j = i; j >= right+1; j--) {
<span style="white-space:pre">				</span>a[j+1]=a[j];
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>//插入
<span style="white-space:pre">			</span>a[right+1]=temp;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}

以下是完整代码,实现重载:

package cn.hncu.sort;

public class InsertSort {
	
	//整个排序数列分为有序前列和无序后列
	//将每一个无序后列中的数与前面有序数列倒序一一对比,插入相应位置
	
	//第一种
	public void eliminateinsertSort(int[] a,int type){
		
		//每次循环前0 - i+1 位数据有序
		for (int i = 0; i < a.length-1; i++) {
			int temp=a[i+1];//对a[i+1]排序位子
			boolean isInsertSort=false;
			for (int j = i; j >= 0; j--) {
				if(type==0){//从小到大排
					if(temp<a[j])//大数向后移动
						a[j+1]=a[j];
					else{
						a[j+1]=temp;//插入
						isInsertSort=true;
					}
				}else{//从大到小排
					if(temp>a[j])//小数向后移动
						a[j+1]=a[j];
					else{
						a[j+1]=temp;//插入
						isInsertSort=true;
					}
				}
				if(isInsertSort)//插入时break
					break;
			}
			if(!isInsertSort)//当前面皆为小数/大数时 未发生插入,则放第一个
				a[0]=temp;
		}
	}

	//优化
	public void eliminateinsertSort2(int[] a,int type){
		for (int i = 0; i < a.length-1; i++) {
			int temp=a[i+1];
			int j=i;
			if (type==0) {//从小到大
				while (a[j] > temp) {//循环,找到位置
					a[j+1]=a[j];//大数后移
					j--;
					if(j<0)
						break;
				}
				a[j+1]=temp;//插入
			}else{//从大到小
				while (a[j] < temp) {
					a[j+1]=a[j];//小数后移
					j--;
					if(j<0)
						break;
				}
				a[j+1]=temp;
			}
		}
	}
	//利用二分搜索优化插排
<span style="white-space:pre">	</span>//插排 前面是有序序列,从后往前一个个比较太慢
<span style="white-space:pre">	</span>//对于大量数据来说,慢,采用二分,快速定位位置
<span style="white-space:pre">	</span>public void insertSort(double[] a,int type){
<span style="white-space:pre">		</span>for (int i = 0; i < a.length-1; i++) {
<span style="white-space:pre">			</span>double temp=a[i+1];
<span style="white-space:pre">			</span>int left=0;
<span style="white-space:pre">			</span>int right=i;
<span style="white-space:pre">			</span>while(left<=right){//定位
<span style="white-space:pre">				</span>int mid=(left+right)/2;
<span style="white-space:pre">				</span>if(temp>a[mid]){
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>left=mid+1;
<span style="white-space:pre">					</span>}else{
<span style="white-space:pre">						</span>right=mid-1;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}else{
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>right=mid-1;
<span style="white-space:pre">					</span>}else{
<span style="white-space:pre">						</span>left=mid+1;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>//移动
<span style="white-space:pre">			</span>for (int j = i; j >= right+1; j--) {
<span style="white-space:pre">				</span>a[j+1]=a[j];
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>//插入
<span style="white-space:pre">			</span>a[right+1]=temp;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public void insertSort2(double[] a,int type){
<span style="white-space:pre">		</span>//一样,第0位有序,对1~a.length-1进行插排
<span style="white-space:pre">		</span>for (int i = 0; i < a.length-1; i++) {
<span style="white-space:pre">			</span>double temp=a[i+1];
<span style="white-space:pre">			</span>//开始二分定位
<span style="white-space:pre">			</span>int k=pitch(a,0,i,temp,type);
<span style="white-space:pre">			</span>for (int j = i+1; j >k; j--) {
<span style="white-space:pre">				</span>a[j]=a[j-1];
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>a[k]=temp;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
	//a[first]<...<a[k]<...<a[end]
	//first~end有序,a[k]定位
	private int pitch(double[] a, int first, int end, double temp,int type) {
		
		int min=(first+end)/2;
		if(type==0){//从小到大排
			//二分的最终结果只有二种
			//所在一侧有俩个数或一个数
			
			//当有俩个数时,
			//要么比first小,则确定位置
			if(first==end-1){
				if(a[first]>temp)
					return first;
				//否则递归至下一次 first==end处理
			}
			//当有一个数时first==end
			//此时要么比first大,返回+1,否则first
			else if(first==end){
				if(a[first]>temp)
					return first;
				else
					return first+1;
			}
			if(a[min]>temp)//temp在a数组a[min]之前
				return pitch(a,first,min-1,temp,type);
			else//temp在a数组a[min]之后
				return pitch(a,min+1,end,temp,type);
		}else{//从大到小排
			if(first==end-1){
				if(a[first]<temp)
					return first;
			}
			else if(first==end){
				if(a[first]<temp)
					return first;
				else
					return first+1;
			}
			if(a[min]<temp)
				return pitch(a,first,min-1,temp,type);
			else
				return pitch(a,min+1,end,temp,type);
		}
	}

	//默认从小到大排
	public void insertSort(double[] a){
		insertSort(a,0);
	}

	//对某一段排序
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(double[] a,int first,int end){
		return insertSort(a, first, end, 0);
	}
	
	//float[]数组
	public void insertSort(float[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(float)b[i];
		}
	}
	public void insertSort(float[] a){
		insertSort(a, 0);
	}
	public boolean insertSort(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);//截取
		insertSort(b, type);//排序
		for (int i = 0; i < b.length; i++) {//放回
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(float[] a,int first,int end) {
		return insertSort(a, first, end, 0);
	}
	//long[]数组
	public void insertSort(long[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(long)b[i];
		}
	}
	public void insertSort(long[] a){
		insertSort(a, 0);
	}
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(long[] a,int first,int end) {
		return insertSort(a, first, end, 0);
	}
	//int[]数组
	public void insertSort(int[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(int)b[i];
		}
	}
	public void insertSort(int[] a){
		insertSort(a, 0);
	}
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(int[] a,int first,int end) {
		return insertSort(a, first, end, 0);
	}
	//short[]数组
	public void insertSort(short[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(short)b[i];
		}
	}
	public void insertSort(short[] a){
		insertSort(a, 0);
	}
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(short[] a,int first,int end) {
		return insertSort(a, first, end, 0);
	}
	//byte[]数组
	public void insertSort(byte[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(byte)b[i];
		}
	}
	public void insertSort(byte[] a){
		insertSort(a, 0);
	}
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(byte[] a,int first,int end) {
		return insertSort(a, first, end, 0);
	}
	//char[]数组
	public void insertSort(char[] a,int type){
		double[] b=new double[a.length];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i];
		}
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[i]=(char)b[i];
		}
	}
	public void insertSort(char[] a){
		insertSort(a, 0);
	}
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(char[] a,int first,int end) {
		return insertSort(a, first, end, 0);
	}
	//String[]数组
	public void insertSort(String[] a,int type){
<span style="white-space:pre">		</span>for (int i = 0; i < a.length-1; i++) {
<span style="white-space:pre">			</span>String temp=a[i+1];
<span style="white-space:pre">			</span>int left=0;
<span style="white-space:pre">			</span>int right=i;
<span style="white-space:pre">			</span>while(left<=right){
<span style="white-space:pre">				</span>int mid=(left+right)/2;
<span style="white-space:pre">				</span>if(temp.compareTo(a[mid])>0){
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>left=mid+1;
<span style="white-space:pre">					</span>}else{
<span style="white-space:pre">						</span>right=mid-1;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}else{
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>right=mid-1;
<span style="white-space:pre">					</span>}else{
<span style="white-space:pre">						</span>left=mid+1;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>for (int j = i; j >= right+1; j--) {
<span style="white-space:pre">				</span>a[j+1]=a[j];
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>a[right+1]=temp;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}

	//默认从小到大排
	public void insertSort(String[] a){
		insertSort(a,0);
	}

	//对某一段排序
	public boolean insertSort(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);
		insertSort(b, type);
		for (int i = 0; i < b.length; i++) {
			a[first+i]=b[i];
		}
		return true;
	}
	public boolean insertSort(String[] a,int first,int end){
		return insertSort(a, first, end, 0);
	}
	//交换
	private void swap(int[] a, int j, int i) {
		//借助第三变量
		int 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 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();
	}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值