Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

m
better formatting of the pseudocode
(formatting)
m (better formatting of the pseudocode)
Line 3:
An [[O]](n<sup>2</sup>) sorting algorithm which moves elements one at a time into the correct position. The algorithm is as follows (from the [[wp:Insertion_sort#Algorithm|wikipedia]]):
'''function''' ''insertionSort''(array A)
'''for''' i ='''from''' 1 '''to''' length[A]-1 '''do'''
value := A[i]
j := i-1
'''while''' j >= 0 '''and''' A[j] > value '''do'''
A[j + 1] := A[j]
j := j-1
'''done'''
A[j+1] = value
'''done'''
 
Writing the algorithm for integers will suffice.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.