算法总结

package sort;

/**
 * 排序算法实现
 * @author tangxing
 *
 */
public class Sort {

	/**
	 * 冒泡排序
	 * @param a
	 */
	public static void bubbleSort(int[] a){
		for(int i=0;i<a.length-1;i++){
			for(int j=0;j<a.length-i-1;j++){
				if(a[j]>a[j+1]){
					int temp = a[j+1];
					a[j+1] = a[j];
					a[j] = temp;
				}
			}
		}
	}
	
	/**
	 * 快速排序,
	 * 思想:分治法,选取一个基准元素,一趟排序之后分为两组,一组中的所有元素比基准元素小,另一组中的所有元素比基准元素大,然后用递归将两组中的元素继续排序
	 * @param a
	 * @param low
	 * @param high
	 */
	public static void quickSort(int[] a,int low,int high){
		if(low >= high){
			return;
		}
		int index = partition(a,low,high);
		quickSort(a,low,index-1);
		quickSort(a,index+1,high);
	}
	
	/**
	 * 快排的partition函数
	 * @param a
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int partition(int[] a,int begin,int end){
		int base = a[begin];	//取数组中第一位为基准元素
		int low = begin;
		int high = end;
		while(low < high){
			//从右向左开始扫描
			while(low<high && base<a[high]){
				high--;
			}
			a[low] = a[high];
			//从左向右开始扫描
			while(low<high && base>a[low]){
				low++;
			}
			a[high] = a[low];
		}
		a[low] = base;//将基准元素放到中间位置
		return low;//返回中间位置的索引
	}
	
	/**
	 * 归并算法
	 * @param a
	 * @param first
	 * @param last
	 * @param tmp
	 */
    public static void mergeSort(int a[],int first,int last,int tmp[]){
        if(first<last){
            int mid=(first+last)/2;
            mergeSort(a, first, mid, tmp);//左边有序
            mergeSort(a, mid+1, last, tmp);//右边有序
            merge(a, first, mid, last, tmp);//合并
        }
    }
	
    //将有序数组a[first..mid] a[mid+1,last]合并
    public static void merge(int a[],int first,int mid,int last,int tmp[]){
        int i=first;//前一个数组的开始下标
        int j=mid+1;//后一个数组的开始下标
        int m=mid;//前一个数组的最后下标
        int n=last;//后一个数组的最后下标
        int k=0;//存放临时数组到tmp
        while(i<=m&&j<=n){
            if(a[i]<=a[j]){
                tmp[k++]=a[i];
                i++;
            }else {
                tmp[k++]=a[j];
                j++;
            }
        }
        while(i<=m){
            tmp[k++]=a[i++];
        }
        while(j<=n){
            tmp[k++]=a[j++];
        }
        //复制tmp到a数组
        for(i=0;i<k;i++){
            a[first+i]=tmp[i];
        }

    }
	
	public static void main(String[] args) {
		//冒泡排序
		int[] a = {5,9,8,1,5,7,3,2,6};
		bubbleSort(a);
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println("\n--------------------------");
		//快速排序
		int[] b = {5,9,8,1,7,3,2,6};
		quickSort(b,0,b.length-1);
		for(int i=0;i<b.length;i++){
			System.out.print(b[i]+" ");
		}
		System.out.println("\n--------------------------");
		//归并排序
		int[] c = {5,9,8,1,5,7,3,2,6};
		int tmp[]=new int[c.length];
        mergeSort(c, 0, c.length-1, tmp);
		for(int i=0;i<c.length;i++){
			System.out.print(c[i]+" ");
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值