zoukankan      html  css  js  c++  java
  • java

    冒泡排序

        //冒泡排序, type = 0 则从小到大, type = 1则从大到小
        //遍历一遍如果后面的比前面的小(大),则两者交换,遍历length-1遍
        public void bubbleSort(int a[], int type){
            for(int i = 0; i < a.length - 1; i++) {
                for (int j = 0; j < a.length - 1; j++) {
                    if ( (a[j] > a[j + 1] && type==0) || (a[j] < a[j + 1] && type == 1) ){
                        int n = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = n;
                    }
                    //showArray(a);
                }
            }
        }

     选择排序

        //选择排序, type = 0 则从小到大, type = 1则从大到小
        //遍历一遍选出前i个中最小(大)的和a[i-1]交换。i从length到1;
        public void selectionSort(int a[], int type){
            for(int i = a.length-1; i >= 0; i--) {
                int flag = 0;
                for (int j = 0; j <= i; j++) {
                    if ( (a[flag] > a[j] && type==0) || (a[flag] < a[j] && type == 1) ){
                        flag = j;
                    }
                    //showArray(a);
                }
                int n = a[i];
                a[i] = a[flag];
                a[flag] = n;
            }
        }

    插入排序

        //插入排序, type = 0 则从小到大, type = 1则从大到小
        //从第i个数字开始(i之前的数字都已经排序完毕),如果第i个数字比它前面的大(小),则说明前i个数字没有排好顺序(第i个数字的位置不对),则把第i个数字记录下来,往前遍历,找到合适的位置插入,依次从第2个数字检查到第length个数字。
        public void insertionSort(int a[], int type){
            for(int i = 1; i < a.length; i++){
                int current = a[i];
                int j = i;
                while( (j>0 && current < a[j-1] && type == 0) || (j>0 && current > a[j-1] && type == 1) ) {//当current比a[j-1]数字小(大)时,说明current应该在这个数字之前,则把这个数字往后挪一格。
                    a[j] = a[j-1];
                    j--;
                }
                a[j] = current; //当循环结束时,说明目前a[j]之前的数字比current小(大),所以current存在a[j]。
            }
        }

    希尔排序

        //希尔排序
        public void shellSort(int[] a){
            //可以理解为把数组根据跨度分成很多数组进行插入排序,不断缩小跨度最后统一。
    
            for(int gap = a.length/2; gap > 0; gap = gap/2){  //间距从length/2开始一直减到0(其实应该是1,但是考虑到java整除问题所以才设为0)
                //然后开始把每隔gap个的数字为一组进行插入排序 (其实就是把插入排序里面的减1全变成减gap- -)
                for(int i = gap; i < a.length; i++){
                    int current = a[i];
                    int j = i;
                    while( j - gap >= 0 && current < a[j - gap] ){
                        a[j] = a[j - gap];
                        j = j - gap;
                    }
                    a[j] = current;
                }
            }
        }

    快速排序

    https://www.cnblogs.com/clamp7724/p/11842603.html

    桶排序

    https://www.cnblogs.com/clamp7724/p/11852406.html

    二叉排序树

    https://www.cnblogs.com/clamp7724/p/11861860.html

    当然了- -平时做题的时候排序一般直接用工具类:

    int[] a;
    Arrays.sort(a);
  • 相关阅读:
    bzoj-2748 2748: [HAOI2012]音量调节(dp)
    bzoj-2338 2338: [HNOI2011]数矩形(计算几何)
    bzoj-3444 3444: 最后的晚餐(组合数学)
    codeforces 709E E. Centroids(树形dp)
    codeforces 709D D. Recover the String(构造)
    codeforces 709C C. Letters Cyclic Shift(贪心)
    codeforces 709B B. Checkpoints(水题)
    codeforces 709A A. Juicer(水题)
    Repeat Number
    hdu 1003 Max Sum (动态规划)
  • 原文地址:https://www.cnblogs.com/clamp7724/p/11590218.html
Copyright © 2011-2022 走看看