Procedure Insertionsort (A, n)
The Procedure Subalgorithm sorts the elements of an array ‘A’ in an ascending order. The array consists of ‘n’ number of elements. The variable ‘i’ is used for index and ‘t’ is a temporary variable.
Step 1
Repeat step 1, 2 for i ¬ 2, 3, 4, 5, --- n
set t ¬ A[i], p ¬ i – 1
Temporary variable is set to new value, pointer is adjusted
Step 2
loop, comparison
Repeat while (p > 0 and t < A[p])
set A [p +1] ¬ A[p], p ¬ p – 1
End of step 2 loop
Step 3
Inserting element in appropriate place
set A [p + 1] ¬ t
End of step 1 loop
Step 4
Finished
return
No comments:
Post a Comment