常用的排序算法(总结)

冒泡排序

public class BubbleSort {
    public static void bubbleSort(int[] a) {
        if(a.length <= 1){
        	return;
		}
		for(int i = 0; i < a.length; i++){
			boolean flag = false;
			for(int j = 0; j < a.length - i - 1;j++){
				if(a[j] > a[j + 1]){
					int temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
					flag = true;
				}
			}
			if(!flag){
				break;
			}
		}
    }
}

插入排序

public class InsertionSort {
    public static void insertionSort(int[] a){
        if (a.length <= 1) {
            return;
        }
        for(int i = 1; i < a.length; i++){
        	int value = a[i];
        	int j = i - 1;
        	for(;j >= 0;j--){
        		if(a[j] > value){
        			a[j+1] = a[j];
        		}else{
        			break;
        		}
        	]
        	a[j+1] = value;
        }
    }
}

选择排序

public class SelectionSort {
    public static void selectionSort(int[] a) {
        if (a.length <= 1) {
            return;
        }
        for (int i = 0; i < a.length - 1; i++) {
            int min = a[i];
            int index = i;
            for (int j = i + 1; j < a.length; j++) {
                if (min > a[j]){
                    index = j;
                    min = a[j];
                }
            }
            int temp = a[i];
            a[i] = a[index];
            a[index] = temp;
        }
    }
}

计数排序

public class CountingSort {
    public static void countingSort(int[] a){
        if (a.length <= 1) {
            return;
        }
        //查找最大的数 确定桶的大小
        int max = a[0];
        for (int i = 1; i < a.length; i++) {
            if (max < a[i]){
                max = a[i];
            }
        }

        //创建桶数组 并存入桶中
        int[] c = new int[max+1];
        for (int i = 0; i < a.length; i++) {
            c[a[i]]++;
        }

        //对桶中元素进行计数
        for (int i = 1; i < c.length; i++) {
            c[i] = c[i-1] + c[i];
        }

        //对数组进行排序存入临时数组中
        int[] temp = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            int index = c[a[i]] - 1;
            temp[index] = a[i];
            c[a[i]]--;
        }

        //拷贝数组
        for (int i = 0; i < a.length; i++) {
            a[i] = temp[i];
        }

    }
}

归并排序

public class MergeSort {
    public static void mergeSort(int[] a){
        mergeSortion(a,0,a.length-1);
    }

    /**
     * 调用递归完成分治排序
     * @param a
     * @param p
     * @param r
     */
    private static void mergeSortion(int[] a,int p, int r){
        if (p >= r) {
            return;
        }
        int q = p + (r - p) / 2;
        mergeSortion(a,p,q);
        mergeSortion(a,q+1,r);
        merge(a,p,q,r);
    }

    /**
     * 合并数组
     * @param a
     * @param p
     * @param q
     * @param r
     */
    private static void merge(int[] a,int p,int q,int r){
        int i = p;
        int j = q + 1;
        int k = 0;
        int[] temp = new int[r-p+1];
        while (i <= q && j <= r) {
            if (a[i] <= a[j]) {
                temp[k++] = a[i++];
            }else {
                temp[k++] = a[j++];
            }
        }
        //判断哪个子数组中有剩余数据
        int start = i;
        int end = q;
        if (j <= r) {
            start = j;
            end = r;
        }
        //将剩余数据存入临时数组
        while (start <= end) {
            temp[k++] = a[start++];
        }
        //拷贝回原数组
        for (i = 0; i <= r - p; i++) {
            a[p+i] = temp[i];
        }
    }
}

快速排序

public class QuickSort {
    public static void quickSort(int[] a) {
        qS(a,0,a.length-1);
    }

    private static void qS(int[] a,int star,int end) {
        if (star >= end) {
            return;
        }
        int q = partition(a,star,end);
        qS(a,star,q-1);
        qS(a,q+1,end);
    }

    private static int partition(int[] a,int star,int end) {
        int key = a[end];
        int i = star;
        for (int j = star; j < end; j++) {
            if (a[j] < key) {
                if (i == j) {
                    i++;
                }else {
                    int temp = a[i];
                    a[i++] = a[j];
                    a[j] = temp;
                }
            }
        }
        int temp = a[i];
        a[i] = a[end];
        a[end] = temp;
        return i;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值