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.
public static void printArray(List arr) {
StringBuilder sb = new StringBuilder();
for (int num : arr) {
sb.append(num).append(" ");
}
System.out.println(sb.toString().trim());
}
public static void insertionSort1(int n, List<Integer> arr) {
// Write your code here
int i = arr.size()-1;
int j = i-1;
int key = arr.get(i);
while(j>=0 && i>0 && key< arr.get(j)){
arr.set(i, arr.get(j));
printArray(arr);
arr.set(j,key);
i--;
j--;
}
printArray(arr);
}
}
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 →
Java
public static void printArray(List arr) { StringBuilder sb = new StringBuilder(); for (int num : arr) { sb.append(num).append(" "); } System.out.println(sb.toString().trim()); }
}