zoukankan      html  css  js  c++  java
  • 排序的第一天

    归并排序与希尔排序

    希尔排序

    package com.m.suan_pai;
    
    
    import java.util.Arrays;
    
    public class Test {
        public static void main(String[] args) {
            int[] arr = {65, 45, 76, 546, 4, 5, 72, 36, 45, 72, 346, 52};
            xier(arr);
            System.out.println(Arrays.toString(arr));
            //[4, 5, 36, 45, 45, 52, 65, 72, 72, 76, 346, 546]
        }
    
        public static void xier(int[] arr) {
            //1.定义跨度
            int k = 1;
            while (k < arr.length) {
                k = k * 2 + 1;
            }
    
            do {
                k /= 2;
                for (int i = k; i < arr.length; i++) {
                    int temp = arr[i];
                    int j = i - k;
                    while (j >= 0 && arr[j] > temp) {
                        arr[j + k] = arr[j];
                        j -= k;
                    }
                    arr[j + k] = temp;
                }
            } while (k > 1);
    
        }
    
    }
    
    

    归并排序

    package com.m.suan_pai;
    
    
    import java.util.Arrays;
    
    public class Test {
        public static void main(String[] args) {
            int[] arr = {65, 45, 76, 546, 4, 5, 72, 36, 45, 72, 346, 52};
            sort(arr);
            System.out.println(Arrays.toString(arr));
            //[546, 346, 76, 72, 72, 65, 52, 45, 45, 36, 5, 4]
        }
    
        public static void sort(int[] arr) {
            int[] temp = new int[arr.length];
            sort(arr, 0, arr.length - 1, temp);
        }
    
        public static void sort(int[] arr, int left, int right, int[] temp) {
            if (left < right) {
                int mid = (left + right) / 2;
                //左分
                sort(arr, 0, mid, temp);
                //右分
                sort(arr, mid + 1, right, temp);
                //合并
                hebing(arr, left, mid, right, temp);
            }
    
        }
    
    
        public static void hebing(int[] arr, int left, int mid, int right, int[] temp) {
            //临时数组指针
            int index = 0;
            //左指针
            int i = left;
            //右指针
            int j = mid + 1;
    
            while (i <= mid && j <= right) {
                if (arr[j] > arr[i]) {
                    temp[index++] = arr[j++];
                } else {
                    temp[index++] = arr[i++];
                }
            }
    
            while (i <= mid) {
                temp[index++] = arr[i++];
            }
    
            while (j <= right) {
                temp[index++] = arr[j++];
            }
    
            index = 0;
    
            while (left <= right) {
                arr[left++] = temp[index++];
            }
        }
    }
    
    

    希尔排序核心代码,易错点

            
            int i = left;
    
            int j = mid + 1;
    
            int index = 0;
    
            while (i <= mid && j <= right) {
                if (arr[j] > arr[i]) {
                    temp[index++] = arr[j++];
                } else {
                    temp[index++] = arr[i++];
                }
            }
    
    
    
  • 相关阅读:
    2014 中华架构师大会 回想
    mybatis重拾---部署官方demo
    D 语言学习感受
    D语言学习
    D语言简介
    C++输入cin详解
    C++源文件的后缀名问题
    C 函数指针详解
    Linux下的五个查找命令:grep、find、locate、whereis、which
    Qt---QFtp上传、下载二进制文件
  • 原文地址:https://www.cnblogs.com/k-class/p/13705069.html
Copyright © 2011-2022 走看看