Sorting algorithms/Insertion sort: Difference between revisions

m
formatting of header material and adding cross-links
(Added PL/I; added text at top)
m (formatting of header material and adding cross-links)
Line 1:
[[Category:Less Than 20 Examples]]{{task|Sorting Algorithms}}
{{Sorting Algorithm}}
An <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part.
 
Although insertion sort is an <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases (i) small <span style="font-family: serif">''n''</span>, (ii) as the final finishing-off algorithm for <span style="font-family: serif">[[O]](''n'' log ''n'')</span> algorithms such as [[Merge sort|mergesort]] and [[quicksort]].
 
The algorithm is as follows (from the [[wp:Insertion_sort#Algorithm|wikipedia]]):
Anonymous user