直接插入排序(Straight Insertion Sort)代码及分析注释

来源:互联网 发布:淘宝消费总金额怎么看 编辑:程序博客网 时间:2024/05/29 18:59
class StraightInsertionSort {// 直接插入排序    public static int[] SISort(int[] array) {        for (int round = 1; round < array.length; round++)// 控制比较的趟数、指针的起点:从前向后依次插入        {            if (array[round] < array[round - 1]) {// 当待插入数小于前一个数才开始排序,如果大于(或等于)则直接插入了                int index = round;// 记录待插入位置的索引                int tmp = array[round];// 记录需要插入的元素的值                for (; index > 0 && tmp < array[index - 1]; index--) {// 从指针起点index依次向前(index--)比较,直到与array[0]比较(index>0),当待插入数tmp小于前一个数时,则执行...                    array[index] = array[index - 1];// 将前一个数向后挪一位                }                // 当比较完array[0]或待插入值tmp大于前一个数时,将待插入数插入指针位置                array[index] = tmp;            }        }        return array;    }}


0 0
原创粉丝点击