气泡,归并,插入,快速,选择排序java实现

public class SortUtils {
	
	/**
	 * 直接插入排序
	 * @param a 待排序的数组
	 * @param n 待排序数组的个数
	 * @return 有序的数列
	 */
	public static void StraightInsertSort(int[] a,int n){
		long s = System.currentTimeMillis();
		for(int i=1;i<n;i++){
			int t = a[i];
			int j;
			for(j=i-1;j>=0&&t<a[j];j--){
				a[j+1]=a[j];
			}
			a[j+1]=t;
		}
		long e = System.currentTimeMillis();
//		int time =(int) (e-s)/1000;
//		System.out.println("排序耗时:"+time+"秒");
		System.out.println("直接排序耗时:"+(e-s)+"毫秒");
	}
	
	/**
	 * 
	 * @param a 待排序的数组
	 * @param n 待排序的数组长度
	 * @param inc shell排序的数组
	 * @param t 排序数组的长度
	 */
	public static void ShellSort(int[] a,int n,int inc[],int t){
		long s = System.currentTimeMillis();
		for(int i=1;i<t;i++){
			ShellInsert(a,n,inc[i]);
		}
		long e = System.currentTimeMillis();
		System.out.println("希尔排序耗时:"+(e-s)+"毫秒");
	}
	
	private static void ShellInsert(int[] a, int n, int inc) {
		for(int i =inc;i<n;i++){
			int j;
			int temp = a[i];
			for(j=i-inc;j>0&&temp<a[j];j-=inc){
				a[j+inc]=a[j];
			}
			a[j+inc]=temp;
		}
	}
	
	public static void BubbleSort(int [] a,int len){
		long s = System.currentTimeMillis();
		int j;
		for(int i=len-1;i>0;i--){//i的起始值为len-1
			for(j=0;j<i;j++){
				if(a[j]>a[j+1]){
					Swap(a[j],a[j+1]);
				}
			}
		}
		long e = System.currentTimeMillis();
		System.out.println("起泡排序耗时:"+(e-s)+"毫秒");
	}
	private static void Swap(int i,int j){
		int temp;
		temp=i;
		i=j;
		j=temp;
	}
	public static void QuickSort(int [] a,int len){
		long s = System.currentTimeMillis();
		QuickSortHelp(a,0,len-1);
		long e = System.currentTimeMillis();
		System.out.println("快速排序耗时:"+(e-s)+"毫秒");
	}
	private static void QuickSortHelp(int []a,int low,int high){
		if(low<high){
			int i = Partition(a,low,high);
			QuickSortHelp(a,low,i-1);
			QuickSortHelp(a,i+1,high);
		}
	}
	
	/**
	 * 
	 * @param a
	 * @param low
	 * @param high
	 * @return
	 */
	private static int Partition(int[] a,int low,int high){
		if(low<high){
			while(low<high&&a[low]<=a[high]){
				high--;
			}
			Swap(a[low],a[high]);
			while(low<high&&a[low]<=a[high]){
				low++;
			}
			Swap(a[low],a[high]);
		}
		return low;
	}
	
	//简单选择排序
	/**
	 * 
	 * @param a
	 * @param n
	 */
	public static void SimpleSelectSort(int[] a,int n){
		long s = System.currentTimeMillis();
		//选择最小的一个元素,循环len-1次,最后一个数一定是最大的
		for(int i=0;i<n-1;i++){
			int minIndex=i;
			for(int j=i+1;j<n;j++){
				if(a[j]<a[minIndex]){
					minIndex=j;
				}
			}
			Swap(a[i],a[minIndex]);
		}
		long e = System.currentTimeMillis();
		System.out.println("简单选择排序耗时:"+(e-s)+"毫秒");
	}
	
	//归并排序核心参数,五个参数,三个临时变量,三次循环
	private static void Merge(int[] a,int[] temp,int low,int mid,int high){
		int i =low;
		int j=mid;
		int index=0;
		while(i<mid&&j<=high){
			if(a[i]<a[j]){
				temp[index++]=a[i++];
			}else{
				temp[index++]=a[j++];
			}
		}
		while(i<mid){
			temp[index++]=a[i++];
		}
		while(j<=high){
			temp[index++]=a[j++];
		}
		for(i=0;i<index;i++){
			a[i]=temp[i];
		}
	}
	
	//归并排序辅助函数,递归函数,递归条件是low<high
	//递归条件,两次递归,一次归并
	private static void MergeSortHelp(int[] a,int[] temp,int low,int high){
		int mid;
		if(low<high){
			mid =(low+high)/2;
			MergeSortHelp(a,temp,low,mid);
			MergeSortHelp(a,temp,mid+1,high);
			//不能是下面这样
//			MergeSortHelp(a,temp,low,mid-1);
//			MergeSortHelp(a,temp,mid,high);
			Merge(a,temp,low,mid,high);
		}
	}
	
	//归并排序
	public static void MergeSort(int[] a,int len){
		long s = System.currentTimeMillis();
		int[] tem=new int[2300];
		MergeSortHelp(a,tem,0,len-1);
		long e = System.currentTimeMillis();
		System.out.println("归并排序耗时:"+(e-s)+"毫秒");
	}

参考 《算法》第4版

都是内排序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值