排序算法-插入排序(insertion sort)

算法描述

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

  • 从第一个元素开始,该元素可以认为已经被排序;
  • 取出下一个元素,在已经排序的元素序列中从后向前扫描;
  • 如果该元素(已排序)大于新元素,将该元素移到下一位置;
  • 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置;
  • 将新元素插入到该位置后;
  • 重复步骤2~5。

动图演示

代码实现

private static void sort(int[] arr) {
    int length = arr.length;
    int preIndex, current;
    for (int i = 1; i < length; i++) {
        preIndex = i - 1;
        current = arr[i];
        while (preIndex >= 0 && arr[preIndex] > current) {
            arr[preIndex + 1] = arr[preIndex];
            preIndex--;
            count++;
        }
        arr[preIndex + 1] = current;
    }
}

猜你喜欢

转载自www.cnblogs.com/StivenYang/p/13189305.html
今日推荐