We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticvoidinsertionSort1(intn,List<Integer>arr){intv=arr.get(n-1);// take last numberinti=n-2;// get last but one indexwhile(i>=0&&v<arr.get(i)){// continue the loop till i>=0 and value < ith valueif(arr.get(i)>v)arr.set(i+1,arr.get(i));// if value < ith value, shift ith value to i+1 th indexi--;// go to leftfor(intx:arr)System.out.print(x+" ");// print the array in each setpSystem.out.println();}arr.set(i+1,v);// at ent set i+th value to value selected in the beginingfor(intx:arr)System.out.print(x+" ");// print the array againSystem.out.println();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insertion Sort - Part 1
You are viewing a single comment's thread. Return to all comments →
Simple java solution