排序算法

4 篇文章 0 订阅

不错的算法文章

http://www.cnblogs.com/eniac12/p/5329396.html

1,交换排序 

1)冒泡排序

尽管是最容易理解的排序算法之一,但他对于少数元素之外的数列排序是很没有效率的

 //冒泡排揎
    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }


2)快速排序

    //快速排序
    private static void quickSort(int[] arr, int left, int right) {
        int i, j, t, temp;
        if (left > right) {
            return;
        }

        temp = arr[left];//temp中存放的是基准数
        i = left;
        j = right;
        while (i != j) {
            //先从有病开始找
            while (arr[j] >= temp && i < j) {
                j--;
            }
            //在找左边
            while (arr[i] <= temp && i < j) {
                i++;
            }
            //交换两个数在数组中的位置
            if (i < j) {
                t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }
        //最终将基数归位
        arr[left] = arr[i];
        arr[i] = temp;

        quickSort(arr, left, i - 1);//继续处理左边的
        quickSort(arr, i + 1, right);//继续处理右边的
    }

2,插入排序

1)插入排序

 //插入排序
    private static void insertionSort(int[] arr){
        int preIndex,current;
        for (int i = 1; i < arr.length; i++){
            preIndex = i - 1;
            current = i;
            while (preIndex >= 0 && arr[preIndex] > current){
                arr[preIndex + 1] = arr[preIndex];
                preIndex --;
            }
            arr[preIndex + 1] = current;
        }
    }

2)希尔排序

 //希尔排序
    private static void shellSort(int[] arr) {
        int gap = (int) Math.ceil(arr.length / 2d); //对浮点数向上取整
        while (gap > 0){
            for (int i = 0; i < arr.length; i++){
                int j = i;
                int temp = arr[i];
                while (j >= gap && arr[j - gap] > temp){
                    arr[j] = arr[j-gap];
                    j = j - gap;
                }
                arr[j] = temp;
            }
            gap = (int)Math.floor(0.5 + gap / 2.2); //对浮点数向下取整
        }
    }

3,选择排序

1)选择排序

//选择排序
    private static void selectionSort(int[] arr) {
        int minIndex, temp;
        for (int i = 0; i < arr.length - 1; i++) {
            minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]){ //寻找最小的数
                    minIndex = j;    //将最小的数索引保存
                }
            }
            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

2)堆排序

 //堆排序
    private static void heapSort(int[] arr){
        buildMaxHeap(arr); //建立大根堆
        for (int i = arr.length - 1; i >= 1; i--){
            int max = arr[0];
            arr[0] = arr[i];
            arr[i] = max;

            maxHeapity(arr,0,i - 1);
        }
    }

    private static void buildMaxHeap(int[] arr){
        for (int i = (arr.length / 2) - 1; i > 0; i--){
            maxHeapity(arr,i,arr.length - 1);
        }
    }

    private static void maxHeapity(int[] arr,int root,int bottom){
        int rootValue = arr[root];
        int maxChild = root * 2 + 1;//start from left child
        while (maxChild <= bottom){
            if (maxChild < bottom){
                if (arr[maxChild] < arr[maxChild + 1]){
                    maxChild = maxChild + 1;
                }
            }
            if (rootValue < arr[maxChild]){
                arr[root] = arr[maxChild];
                root = maxChild;
                maxChild = root * 2 + 1;
            } else {
                maxChild = bottom + 1;
            }
            arr[root] = rootValue;
        }
    }





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值