insertion sort(插入排序)
2017-02-04 14:18
316 查看
转载自:http://java2novice.com/java-sorting-algorithms/insertion-sort/
Insertion sort is a simple sorting algorithm, it builds the final sorted array one item at a time. It is much less efficient on large lists than other sort algorithms.
Advantages of Insertion Sort:
1) It is very simple.
2) It is very efficient for small data sets.
3) It is stable; i.e., it does not change the relative order of elements with equal keys.
4) In-place; i.e., only requires a constant amount O(1) of additional memory space.
Insertion sort iterates through the list by consuming one input element at each repetition, and growing a sorted output list. On a repetition, 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.
Image source: “Introduction to Algorithms”, The MIT Press
The best case input is an array that is already sorted. In this case insertion sort has a linear running time (i.e., Θ(n)). During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection
of the array. The simplest worst case input is an array sorted in reverse order. The set of all worst case inputs consists of all arrays where each element is the smallest or second-smallest of the elements before it. In these cases every iteration of the
inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. This gives insertion sort a quadratic running time (i.e., O(n2)). The average case is also quadratic, which makes insertion sort impractical for sorting
large arrays. However, insertion sort is one of the fastest algorithms for sorting very small arrays, even faster than quicksort; indeed, good quicksort implementations use insertion sort for arrays smaller than a certain threshold, also when arising as subproblems;
the exact threshold must be determined experimentally and depends on the machine, but is commonly around ten.
- See more at: http://java2novice.com/java-sorting-algorithms/insertion-sort/#sthash.NxLpBJ36.dpuf
Insertion sort is a simple sorting algorithm, it builds the final sorted array one item at a time. It is much less efficient on large lists than other sort algorithms.
Advantages of Insertion Sort:
1) It is very simple.
2) It is very efficient for small data sets.
3) It is stable; i.e., it does not change the relative order of elements with equal keys.
4) In-place; i.e., only requires a constant amount O(1) of additional memory space.
Insertion sort iterates through the list by consuming one input element at each repetition, and growing a sorted output list. On a repetition, 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.
Image source: “Introduction to Algorithms”, The MIT Press
The best case input is an array that is already sorted. In this case insertion sort has a linear running time (i.e., Θ(n)). During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection
of the array. The simplest worst case input is an array sorted in reverse order. The set of all worst case inputs consists of all arrays where each element is the smallest or second-smallest of the elements before it. In these cases every iteration of the
inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. This gives insertion sort a quadratic running time (i.e., O(n2)). The average case is also quadratic, which makes insertion sort impractical for sorting
large arrays. However, insertion sort is one of the fastest algorithms for sorting very small arrays, even faster than quicksort; indeed, good quicksort implementations use insertion sort for arrays smaller than a certain threshold, also when arising as subproblems;
the exact threshold must be determined experimentally and depends on the machine, but is commonly around ten.
? |
Output: |
2, 7, 10, 34, 42, 56, 67, 88, |
相关文章推荐
- 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结
- 链表的插入排序 Insertion Sort List
- 插入排序(Insertion Sort)
- C++ insertion sort(插入排序)
- 插入排序(INSERTION-SORT)
- 算法总结JS版(三)—— 插入排序(Insertion Sort)
- 基础排序算法 – 插入排序Insertion sort
- 选择排序(Selection Sort)和插入排序(Insertion Sort)
- Leetcode Insertion Sort List 插入排序链表
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
- [算法练习]Insertion Sort 插入排序
- 排序算法——插入排序(Insertion Sort)
- insertionSort()插入排序
- 插入排序:直接插入排序-Direct insertion sort
- 插入排序(InsertionSort)
- 排序算法--插入排序(Insertion Sort)_C#程序实现
- 插入排序(InsertionSort)
- 插入排序(InsertionSort)
- Python 数据结构与算法——插入排序(insertion sort)
- 插入排序(INSERTION-SORT)