zoukankan      html  css  js  c++  java
  • 4种排序实践

        /**
         * 二分查找
         * @param group
         * @param left
         * @param right
         * @param target
         * @return
         */
        public static int erfen(int [] group, int left, int right, int target) {
            int mid = (left+right)/2;
    
            if(left <= right) {
                int midValue = group[mid];
                if(midValue < target) {
                    return erfen(group, mid+1, right, target);
                } else if(midValue > target){
                    return erfen(group, left, mid-1, target);
                } else {
                    return mid;
                }
            } else {
                return -1;
            }
        }
    
        /**
         * 冒泡排序
         * @param group
         */
        public static void maopao(int [] group) {
            for(int i=0; i<group.length; ++i) {
                for(int j=0; j<group.length-i-1; ++j) {
                    if(group[j] > group[j+1]) {
                        int temp = group[j];
                        group[j] = group[j+1];
                        group[j+1] = temp;
                    }
                }
            }
        }
    
        /**
         * 插入排序
         * @param array
         */
        public static void charu(int[] array){
            for(int i =1;i<array.length;i++){
                int temp = array[i];
                int j = i-1;
                while(j>=0 && temp < array[j] ){
                    array[j+1] = array[j];
                    j--;
                }
                array[j+1] = temp;
            }
        }
    
        /**
         * 选择排序
         * @param group
         */
        public static void xuanze(int [] group) {
            for(int i=0; i<group.length; ++i) {
                int min = 100000000;
                int minIndex = -1;
                for(int j=i; j<group.length; ++j) {
                    if(group[j] < min) {
                        minIndex = j;
                        min = group[j];
                    }
                }
                int temp = group[i];
                group[i] = group[minIndex];
                group[minIndex] = temp;
            }
        }
    
        /**
         * 快速排序
         * @param group
         * @param low
         * @param high
         */
        public static void quickSort(int [] group, int low, int high) {
            if(low < high) {
                int mid = slipGroup(group, low, high);
                quickSort(group, low, mid-1);
                quickSort(group, mid+1, high);
            }
        }
    
        public static int slipGroup(int [] group, int low, int high) {
            int i = low;
            int j = high+1;
            int patter = group[low];
    
            while (true) {
                while (group[++i] < patter) {
                    if(i == high)
                        break;
                }
                while(group[--j] > patter) {
                    if(j == low)
                        break;
                }
    
                if(i >= j)
                    break;
    
                int temp = group[i];
                group[i] = group[j];
                group[j] = temp;
    
            }
    
            group[low] = group[j];
            group[j] = patter;
    
            return j;
    
        }






  • 相关阅读:
    Use MVS Dsbame convensions. windows下ftp.exe客户端上传错误
    Sqlserver 2005:数据库快照
    Oracle:使用ASM自动存储管理, 严重推荐
    Thunderbird 邮件客户端:windows 和 ubuntu 或 liunx 下共用的方法
    Oracle:Oracle 10 RAC 安装群集件的准备工作
    SSH
    STL
    ASP生成静态Html文件技术杂谈
    Nessus:网络和主机漏洞评估程序安装试用
    table 的 id 属性不被 document.getElementById支持
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106572.html
Copyright © 2011-2022 走看看