zoukankan      html  css  js  c++  java
  • 排序算法

     【简单记忆】
    * 冒泡排序是从左边第一个数开始,向右,相邻的两个数比较交换位置
    * 选择排序是从左边第一个数开始,单独和右边所有数比较
    * 插入排序是从左边第二个数开始,单独和左边所有数比较

    * 快速排序是每轮设置一个关键值,按照右左右左的方向将比关键值小的排列在其左边,将比关键值大的排列在其右边

    1.冒泡排序

    //数组总长度
    int length = array.length;
    //length -1 轮
    for(int i = 1;i<length;i++){
        //从第二个数开始交换
        for(int focus = 1;focus < length;focus ++){
            //交换实现左小右大
            if(array[focus -1 ]>array[focus]){
                int temp = array[focus];
                array[focus] = array[focus - 1];
                array[focus -1 ] = temp;
            }
        }
        
    }

    2.选择排序

    // 从最左边开始
    for (int i = 0; i < array.length - 1; i++) {
        // 依次将i右边的数和min进行比较
        for (int j = i + 1; j < array.length; j++) {
            if (array[j] < array[i]) {
                int temp = array[j];
                array[j] = array[i];
                array[i] = temp;
            }
        }
    }

    3.插入排序

    //从左边第二个数开始
    for(int i = 1;i<array.length;i++){
        //依次将i和左边的数比较,实现左边小右边大
        for(int j = 0;j<i;j++){
            if(array[i]<array[j]){
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    4.快速排序

    public class QuickSort {
        public static void main(String[] args) {
            int [] array = {49,38,65,97,76,13,27};
            quickSort(array, 0, array.length - 1);
            //打印最终结果
            for (int i = 0; i < array.length; i++) {
                System.out.println(array[i]);
            }
        }
        /*先按照数组为数据原型写出算法,再写出扩展性算法。数组{49,38,65,97,76,13,27}
        * */
        public static void quickSort(int[]n ,int left,int right){
            int pivot;
            if (left < right) {
                //pivot作为枢轴,较之小的元素在左,较之大的元素在右
                pivot = partition(n, left, right);
                //对左右数组递归调用快速排序,直到顺序完全正确
                quickSort(n, left, pivot - 1);
                quickSort(n, pivot + 1, right);
            }
        }
    
        public static int partition(int[]n ,int left,int right){
            int pivotkey = n[left]; 
            //每轮排序的枢轴不变,一般取第一个值,最终枢轴在中间,前小后大
            while (left < right) {
                //每次排序先从右往左,将一个小于枢轴的数放在枢轴的位置上
                while (left < right && n[right] >= pivotkey){
                   --right;
                }
                n[left] = n[right];
                //然后从枢轴目前所在位置往右,将一个大于枢轴的数放在上面的right位置上
                while (left < right && n[left] <= pivotkey){
                   ++left;
                }
                n[right] = n[left];
            }
            //当最终left和right位置重合时,完成一趟快速排序,此时left位相当于空,等待pivotkey补上
            n[left] = pivotkey;
            return left;
        }
    }

     

  • 相关阅读:
    【基础算法】- 全排列
    【基础算法】- 2分查找
    区块链培训
    Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
    test
    No data is deployed on the contract address!
    "throw" is deprecated in favour of "revert()", "require()" and "assert()".
    Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning.
    京都行
    Failed to write genesis block: database already contains an incompatible
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5225935.html
Copyright © 2011-2022 走看看