top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How insertion sort works and in real life where it is used ?

+1 vote
How insertion sort works and in real life where it is used ?
posted Sep 5, 2013 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Insertion sort is one of the basic sorting algorithm. Here I am assuming arr[0] will be smallest and arr[n] will be highest value element in array after sorting. while using this sorting algorithm, we should pick one element from left to right in each iteration and then place that element in proper place. selection sort is just reverse,in selection you focus on position and put proper element but in insertion sort, you focus on element and put in proper place.

In every iteration of insertion sort , left side sub array become sorted. suppose you complete 1st iteration then first two elements for array get sorted. then in 2nd iteration you pick third element arr[2] compare with already sorted sub array element a[1] . if arr[2] is already big than arr[1]. then stop and do next iteration. if arr[2] is smaller than arr[1] then copy arr[1] into arr[2] then again compare arr[2] against arr[0] if arr[2] is larger than arr[0] then copy arr[2] into arr[1] otherwise copy arr[0] into arr[1] else copy arr[2] into arr[1] position.

In real life, Playing cards uses insertion sort to arrange the cards in sequence.

answer Sep 5, 2013 by Vimal Kumar Mishra