重拾 DSA 排序算法之插入排序
wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
意思就是从输入的元素里每次取出一个插入到有序的列表中,直到取完所有的元素为止。
以下是代码实现:
1 | private static void insertionSort(int[] array) { |
算法的时间复杂度为 O(n^2)。