## Sorting – Insertion sort

Insertion Sort Sorts a set of records by comparing the next record with all the previous elements. Compare next element with previous. If next element is smaller than previous element, then swap both the elements. i.e. if(arr[j]>arr[i]) swap(arr[j],arr[i); Example: The loop may be written as follows: for I = 2 to n for j = I-1 to >=1 if(arr[I] < arr[j]) swap(arr[i],arr[j]); Insertion sort is considered best if most of…

Read more...## Sorting – Bubble sort

are closed

Bubble sort: Two consecutive elements are compared. It sorts the elements in right to left fashion. Thus, (n-i) comparisons are made. Example: Frequency: i j number of times ——————————————————————————————— 1 1 to n-i n-1 2 1 to n-i n-2 3 1 to n-i n-3 – …

Read more...## Sorting – Shell sort

are closed

Shell Sort: Shell sort is quite similar to that of insertion sort with the only difference that in shell sort, higher values of k are considered. Whereas insertion sort assumes k to be 1. If k=4, then every 4th element is compared, if it is 3 then every 3rd element is compared. Thus k=m means every mth element gets compared with each other and is swapped. The value of k…

Read more...
are closed