• Java InsertionSort


    Java InsertionSort

    /**
     * <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;
    
    /**
     * Insertion sort is a simple sorting algorithm: a comparison sort in which the
     * sorted array (or list) is built one entry at a time. It is much less
     * efficient on large lists than more advanced algorithms such as quicksort,
     * heapsort, or merge sort. 
     * <p>
     * Family: Insertion.<br>
     * Space: In-place.<br>
     * Stable: True.<br>
     * <p>
     * Average case = O(n^2)<br>
     * Worst case = O(n^2)<br>
     * Best case = O(n)<br>
     * <p>
     * @see <a href="https://en.wikipedia.org/wiki/Insertion_sort">Insertion Sort (Wikipedia)</a>
     * <br>
     * @author Justin Wetherell <phishman3579@gmail.com>
     */
    public class InsertionSort<T extends Comparable<T>> {
    
        private InsertionSort() { }
    
        public static <T extends Comparable<T>> T[] sort(T[] unsorted) {
            int length = unsorted.length;
            for (int i = 1; i < length; i++) {
                sort(i, unsorted);
            }
            return unsorted;
        }
    
        private static <T extends Comparable<T>> void sort(int i, T[] unsorted) {
            for (int j = i; j > 0; j--) {
                T jthElement = unsorted[j];
                T jMinusOneElement = unsorted[j - 1];
                if (jthElement.compareTo(jMinusOneElement) < 0) {
                    unsorted[j - 1] = jthElement;
                    unsorted[j] = jMinusOneElement;
                } else {
                    break;
                }
            }
        }
    }
    

      

  • 相关阅读:
    快速排序算法(c#)
    NHibernate 中createSqlQuery的执行
    Asp.net页面下客户端按钮提交页面到其他Action
    希尔(插入)排序 c#代码
    Asp.net MVC 中冒号的作用
    Net注册JS的几种方式和区别
    Asp.net MVC 使用json数据格式交互
    DataSet的手工创建
    反射基础
    uva10082 WERTYU
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9243213.html
Copyright © 2020-2023  润新知