• 常见的排序算法


    常见排序算法有

    1. 选择排序

    2. 插入排序

    3. 冒泡排序

    4. 快速排序

    5. 归并排序

    这里写了5种排序的java demo,还有很多排序,希尔排序,计数排序,堆排序,基数排序等

    Sort.java

    public class Sort {
        public static void main(String[] args) {
            int[] arr = {3, 6, 9, 2, 5, 4};
            int[] temp = new int[arr.length];
            mergeSort(arr, 0, arr.length - 1, temp);
    //        insertSort(arr);
    //        selectSort(arr);
    //        bubboSort(arr);
    //        quickSort(arr, 0, arr.length - 1);
            for(int i : arr){
                System.out.print(i + ", ");
            }
        }
    
        public static void mergeSort(int[] array, int start, int end, int[] temp){
            if(start < end){
                int middle = (end + start) / 2;
                mergeSort(array, start, middle, temp);
                mergeSort(array, middle + 1, end, temp);
                mergeArray(array, start, middle, end, temp);
    
            }
    
        }
    
        private static void mergeArray(int[] array, int start, int middle, int end, int[] temp){
            int tempIndex = 0;
            int s = start;
            int m = middle + 1;
            int e = end;
    
            while(s <= middle && m <= e){
                if(array[s] <= array[m])
                    temp[tempIndex ++] = array[s ++];
                else
                    temp[tempIndex ++] = array[m ++];
            }
            while(s <= middle)
                temp[tempIndex ++] = array[s ++];
            while(m <= e)
                temp[tempIndex ++] = array[m ++];
            for(int i = 0; i < tempIndex; i++)
                array[start + i] = temp[i];
    
        }
    
        public static void insertSort(int[] array){
            for(int i = 0; i < array.length; i++){
                for(int j = i + 1; j > 0 && j< array.length; j--){
                    if(array[j] < array[j - 1]){
                        int temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    
        public static void selectSort(int[] array){
            for(int i = 0; i < array.length; i ++){
                int maxIndex = i;
                for(int j = i + 1; j < array.length; j ++){
                    maxIndex = array[maxIndex] < array[j] ? j : maxIndex;
                }
                if(maxIndex != i){
                    int temp = array[i];
                    array[i] = array[maxIndex];
                    array[maxIndex] = temp;
                }
            }
        }
    
        public static void bubboSort(int[] array){
            boolean isExchanged = true;
            for(int i = array.length - 1; i > 0 && isExchanged; i --){
                isExchanged = false;
                for(int j = 0; j < i; j ++){
                    if(j < array.length - 1 && array[j] > array[j + 1]){
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                        isExchanged = true;
                    }
                }
            }
        }
    
    
        public static void quickSort(int[] array, int l, int r){
            if(l < r){
                int s = l;
                int e = r;
                int pivotValue = array[l];
                while(l < r){
                    while(l < r && array[r] > pivotValue){
                        r --;
                    }
                    if(l < r)
                        array[l ++] = array[r];
                    while(l < r && array[l] < pivotValue){
                        l ++;
                    }
                    if(l < r)
                        array[r --] = array[l];
                } //while
                array[l] = pivotValue;
                quickSort(array, s, l - 1);
                quickSort(array, l + 1, e);
            }
    
        }
    }

     堆排序

      public static void heapSort(int[] nums) {
        buildMaxHeap(nums);
        System.out.println("after build heap:");
        ArrayUtil.printIntArray(nums);
        for (int i = nums.length; i > 1; i--) {
          int temp = nums[i - 1];
          nums[i - 1] = nums[0];
          nums[0] = temp;
          downAdjust(nums, 1, i - 1);
        }
      }
    
      //10 8 5 12 9
      private static void buildMaxHeap(int[] nums) {
        for (int i = nums.length / 2; i >= 1; i--) {
          downAdjust(nums, i, nums.length);
        }
      }
      private static void downAdjust(int[] nums, int j, int end) {
          while (j * 2 <= end) {
            int maxIndex = j;
            maxIndex = j * 2 <= end && nums[j - 1] < nums[j * 2 - 1] ? j * 2 : maxIndex;
            maxIndex = j * 2 + 1 <= end && nums[maxIndex - 1] < nums[j * 2] ? j * 2 + 1 : maxIndex;
            if (maxIndex != j) {
              int temp = nums[maxIndex - 1];
              nums[maxIndex - 1] = nums[j - 1];
              nums[j - 1] = temp;
              j = maxIndex;
            } //if
            j = j * 2;
          } //while
      }
    

      

  • 相关阅读:
    面向对象与组合
    异常处理和三级菜单练习
    装饰器和生成器
    序列化模块
    leetcode_498. 对角线遍历
    leetcode_566. 重塑矩阵
    leetcode_59. 螺旋矩阵 II
    leetcode_54. 螺旋矩阵
    leetcode_396. 旋转函数
    leetcode_200. 岛屿数量
  • 原文地址:https://www.cnblogs.com/luckygxf/p/8975732.html
Copyright © 2020-2023  润新知