Inserting Item into sorted Array :


Here A is a sorted array stored in memory. This algo inserts a element a data elements ITEM at ( I + 1)th position . ITEM is compared with each element until it founds its appropriate position.

INSERT_SORT () :
Here A is a sorted linear array ( ascending order ) with N elements. ITEM is to be inserted at appropriate position.

Step I    : [ Initialize counter ]   Set I = N
step II   : Repeat While ( ITEM < A[I] ) and ( I >= 1)
Step III : [ Accessing from last ]   Set A[I+1] = A[I]
Step IV : [ Decreasing Counter ]   Set I = I – 1
[ End of While Loop]
step V   : [Insert Item ]   Set A[I + 1] = ITEM
Step VI : [Reset N]   N = N + 1
Step VII : Exit

Leave a comment