Posts

Showing posts with the label Insertion sort

Insertion sort in C, python

Image
Insertion sort                      Insertion sort is a simple sorting technique. It is almost similar to the way we sort the playing cards. The whole array is split into two parts namely the sorted and unsorted. The elements from the unsorted array are placed at the correct position in the sorted array. The process continues until the entire unsorted array gets sorted out. Procedure : Explanation :           Now let us consider an array [7,1,4,2]. We have to start from the element at index 1 and compare it with all the previous elements and arrange the elements in the sorted format. Continue the same process until we reach the last element, so that the entire array gets sorted. For example,  for the array [7,1,4,2], starting with the element 1 at index 1, compare it with previous element 7 ,as 1 is less than 7 swap the elements 1 and 7. The array becomes [1,7,4,2]. Now move onto the next index position. Compare the element at index 2 with the the previous elements and arrange them acc