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.
Insertion Sort - Part 2
Insertion Sort - Part 2
cdima + 0 comments Pretty badly described algorithm, needs a better description of the steps.
vshantam + 0 comments Python 3 b=int(input()) a=[int(x) for x in input().strip().split(' ')] for i in range(1,len(a)): key=a[i] j=i-1 while j>=0 and a[j]>key: a[j+1]=a[j] j=j-1 a[j+1]=key print(*a)
Do let me know if there is more finer solution in python.
Octowl + 0 comments For those trying this in javaScript, remember that the input is an array of strings.
You need to map the elements to integers using:
arr.map(function(x) { return parseInt(x, 10); });
RodneyShag + 0 comments Java solution - passes 100% of test cases
public static void insertionSortPart2(int[] array) { for (int i = 1; i < array.length; i++) { int j = i; int value = array[i]; while (j >= 1 && array[j-1] > value) { array[j] = array[j-1]; j--; } array[j] = value; printArray(array); } }
Full solution available in my HackerRank solutions.
Let me know if you have any questions.
briancmpbll + 0 comments In the C++ code, why switch from vectors in Part 1 to C arrays in Part 2??
Load more conversations
Sort 392 Discussions, By:
Please Login in order to post a comment