The array is virtually split into a sorted and an unsorted part. Values from the unsorted part is picked and placed at the correct position in the sorted part. Complexity:- Worst-case and average-case:- O ( n 2 ) best case:- O(n) Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class InsertionSort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 }; for ( int i= 1 ; i<arr. length ; i++){ int temp = arr[i]; int j = i - 1 ; while (j>= 0 && arr[j]>temp){ arr[j+ 1 ] = arr[j]; j--; } arr[j+ 1 ] = temp; } for ( int x:arr) System. out . print (x+ " " ); } } Python def insertion_sort(arra...
This blog contains coding/programming and interview questions of MNCs like TCS, Wipro, Infosys, Capgemini, Mindtree, Amazon, Google, Facebook, Flipkart, Cisco, HP, Dell, etc. with placement preparation guides.