Sunday, April 5, 2009

Insertion sort

- is a simple sorting algorithm that is relatively efficient for small lists and mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list. In arrays, the new list and the remaining elements can share the array's space, but insertion is expensive, requiring shifting all following elements over by one.

Run-time Complexity Analysis:
This is efficient and sequential.

Codes:
insertionSort(array A)
begin
for i := 1 to length[A]-1
do
begin
value := A[i];
j := i-1;
while j ≥ 0 and A[j] > value do
beginA[j + 1] := A[j];
j := j-1;
end;
A[j+1] := value;
end;
end;

Application:
Most humans when sorting—ordering a deck of cards, for example—use a method that is similar to insertion sort.

Reference:http://en.wikipedia.org/wiki/Sorting_algorithm#Insertion_sort

No comments:

Post a Comment