• Java ShellSort


    Java ShellSort

    /**
     * <html>
     * <body>
     *  <P> Copyright 1994-2018 JasonInternational </p>
     *  <p> All rights reserved.</p>
     *  <p> Created on 2018年4月10日 </p>
     *  <p> Created by Jason</p>
     *  </body>
     * </html>
     */
    package cn.ucaner.algorithm.sorts;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Shellsort, also known as Shell sort or Shell's method, is an in-place
     * comparison sort. It generalizes an exchanging sort, such as insertion or
     * bubble sort, by starting the comparison and exchange of elements with
     * elements that are far apart before finishing with neighboring elements.
     * Starting with far apart elements can move some out-of-place elements into
     * position faster than a simple nearest neighbor exchange. 
     * <p>
     * Family: Exchanging.<br>
     * Space: In-place.<br>
     * Stable: False.<br>
     * <p>
     * Average case = depends on the gap<br>
     * Worst case = O(n * log^2 n)<br>
     * Best case = O(n)<br>
     * <p>
     * @see <a href="https://en.wikipedia.org/wiki/Shell_sort">Shell Sort (Wikipedia)</a>
     * <br>
     * @author Justin Wetherell <phishman3579@gmail.com>
     */
    public class ShellSort<T extends Comparable<T>> {
    
        private ShellSort() { }
    
        public static <T extends Comparable<T>> T[] sort(int[] shells, T[] unsorted) {
            for (int gap : shells) {
                // Allocate arrays
                List<List<T>> subarrays = new ArrayList<List<T>>(gap);
                for (int i = 0; i < gap; i++) {
                    subarrays.add(new ArrayList<T>(10));
                }
                // Populate sub-arrays
                int i = 0;
                int length = unsorted.length;
                while (i < length) {
                    for (int j = 0; j < gap; j++) {
                        if (i >= length)
                            continue;
                        T v = unsorted[i++];
                        List<T> list = subarrays.get(j);
                        list.add(v);
                    }
                }
                // Sort all sub-arrays
                sortSubarrays(subarrays);
                // Push the sub-arrays into the int array
                int k = 0;
                int iter = 0;
                while (k < length) {
                    for (int j = 0; j < gap; j++) {
                        if (k >= length)
                            continue;
                        unsorted[k++] = subarrays.get(j).get(iter);
                    }
                    iter++;
                }
            }
            return unsorted;
        }
    
        private static <T extends Comparable<T>> void sortSubarrays(List<List<T>> lists) {
            for (List<T> list : lists) {
                sort(list);
            }
        }
    
        /**
         * Insertion sort
         * 
         * @param list
         *            List to be sorted.
         */
        private static <T extends Comparable<T>> void sort(List<T> list) {
            int size = list.size();
            for (int i = 1; i < size; i++) {
                for (int j = i; j > 0; j--) {
                    T a = list.get(j);
                    T b = list.get(j - 1);
                    if (a.compareTo(b) < 0) {
                        list.set(j - 1, a);
                        list.set(j, b);
                    } else {
                        break;
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    搭建微信小程序开发环境
    DOM 的classList 属性
    mui监听多个下拉刷新当前处于哪个选项卡
    mui常用功能链接地址
    css 弹性盒模型Flex 布局
    定义变量let,const
    微信小程序从零开始开发步骤(六)4种页面跳转的方法
    微信小程序从零开始开发步骤(五)轮播图
    展开符和解构赋值
    POJ 3660 Floyd传递闭包
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9243187.html
Copyright © 2020-2023  润新知