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 Advanced Analysis
Insertion Sort Advanced Analysis
Sort by
recency
|
357 Discussions
|
Please Login in order to post a comment
Curious why this solution in Python gets a runtime error for about half of the test cases - I am guessing it's something to do with there being an earlier potential exit point for the loop than simply (s < lenA) or that I shouldn't be using functions like index or min in the solution and instead stick to primitive operations. I'm pretty much a novice, so any advice would help:
Ruby solution:
Just have modify mergesort and count the inversion(shift) .
def insertionSort(arr):#by using mergesort count = 0 if len(arr) > 1: mid = len(arr)//2 left_list = arr[:mid] right_list = arr[mid:] count = count + insertionSort(left_list) count = count + insertionSort(right_list) count = count + merge(left_list,right_list,arr) return count def merge(left_list,right_list,arr): i=0 j=0 k=0 count = 0 mid = len(arr)//2 while i
what the hell , if you are getting wrong answer and if you think your code is correct , then the convert the integer to long in pre written code , they said that the range is in integer's range but it isn't.
The provided template should not expect us to return int. Imagine the case
n, n-1, n-2, n-3, ..., 1
, it will require us n*(n - 1) / 2 swap aka will overflow if you use int for n = 1e5. Converted the return type for the provided code to long solved the problem