implementing merge inversions finder - python

import sys
def get_number_of_inversions(a, b, left, right):
number_of_inversions = 0
if right - left <= 1:
return number_of_inversions
ave = (left + right) // 2
number_of_inversions += get_number_of_inversions(a, b, left, ave)
number_of_inversions += get_number_of_inversions(a, b, ave, right)
#write your code here
return number_of_inversions
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
b = n * [0]
print(get_number_of_inversions(a, b, 0, len(a)))
this code should be implemented only in 'write your code here' area
and what i was trying to do whole day:
i=left
j=ave
k=left
temp_num=0
while i<ave and j<right:
if a[i]<=a[j]:
b[k]=a[i]
i+=1
else:
b[k]=a[j]
j+=1
temp_num += (ave-i)
k+=1
number_of_inversions+=temp_num
and second try not so smart :
left=a[:ave]
right=a[ave:]
result = list()
i,j = 0,0
inv_count = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
elif right[j] < left[i]:
result.append(right[j])
j += 1
inv_count += (len(left)-i)
result += left[i:]
result += right[j:]
number_of_inversions +=inv_count
most problematic for me is:
program begin running with right index one bigger than actually is
first make recursive calls
program don't give back sub arrays instead returns inversions number only
please give me some suggestions how to implement that code

I am not really sure what you are trying to do
But the problem might be here right - left <= 1 .
I believe it should be right <= left

Related

Calculating the number of inversions in a sequence required to arrange it in increasing order [duplicate]

This question already has answers here:
Convert all strings in a list to int
(10 answers)
Closed 1 year ago.
The input is of the form
5
2 3 9 2 9
The output should be the number of inversions that should be done to arrange the sequence in order from smallest to greatest. i.e the new sequence would be 2 2 3 9 9 and to produce this from the input 2 inversions were made.
2
So basically what I think I need to do is, convert the input into an array and then run the following code
k = int(input())
str = input()
def getInvCount(arr, n):
inv_count = 0
for i in range(n):
for j in range(i + 1, n):
if (arr[i] > arr[j]):
inv_count += 1
return inv_count
arr = str.split()
n = len(arr)
print(getInvCount(arr, n))
Alternatively I even tried this code
k = int(input())
def mergeSort(arr, n):
temp_arr = [0]*n
return _mergeSort(arr, temp_arr, 0, n-1)
def _mergeSort(arr, temp_arr, left, right):
inv_count = 0
if left < right:
mid = (left + right)//2
inv_count += _mergeSort(arr, temp_arr,
left, mid)
inv_count += _mergeSort(arr, temp_arr,
mid + 1, right)
inv_count += merge(arr, temp_arr, left, mid, right)
return inv_count
def merge(arr, temp_arr, left, mid, right):
i = left
j = mid + 1
k = left
inv_count = 0
while i <= mid and j <= right:
if arr[i] <= arr[j]:
temp_arr[k] = arr[i]
k += 1
i += 1
else:
temp_arr[k] = arr[j]
inv_count += (mid-i + 1)
k += 1
j += 1
while i <= mid:
temp_arr[k] = arr[i]
k += 1
i += 1
while j <= right:
temp_arr[k] = arr[j]
k += 1
j += 1
for loop_var in range(left, right + 1):
arr[loop_var] = temp_arr[loop_var]
return inv_count
str = input()
arr = str.split()
n = len(arr)
result = mergeSort(arr, n)
print(result)
However the first code gets timed out in some cases and the second one fails there. Can I please get some help?
You probably forgot to convert your input to int before passing it into mergesort:
k = int(input())
def mergeSort(arr, n):
temp_arr = [0]*n
return _mergeSort(arr, temp_arr, 0, n-1)
def _mergeSort(arr, temp_arr, left, right):
inv_count = 0
if left < right:
mid = (left + right)//2
inv_count += _mergeSort(arr, temp_arr,
left, mid)
inv_count += _mergeSort(arr, temp_arr,
mid + 1, right)
inv_count += merge(arr, temp_arr, left, mid, right)
return inv_count
def merge(arr, temp_arr, left, mid, right):
i = left
j = mid + 1
k = left
inv_count = 0
while i <= mid and j <= right:
if arr[i] <= arr[j]:
temp_arr[k] = arr[i]
k += 1
i += 1
else:
temp_arr[k] = arr[j]
inv_count += (mid-i + 1)
k += 1
j += 1
while i <= mid:
temp_arr[k] = arr[i]
k += 1
i += 1
while j <= right:
temp_arr[k] = arr[j]
k += 1
j += 1
for loop_var in range(left, right + 1):
arr[loop_var] = temp_arr[loop_var]
return inv_count
str = input()
arr = list(map(int, str.split()))
n = len(arr)
result = mergeSort(arr, n)
print(result)

Why combining insert sort and quick sort get worse result?

I tried to set a cutoff to combine quick sort and insert sort which using insert sort when n(number of data to sort) is lower than cutoff. However, I found the method didn't work and even worse than before. Why and how to imporve it?
To sort 10e4 random int number, the quick sort with a cutoff(50) takes 0.6s and the method without a cutoff takes only 0.02s.
The quick sort with a cutoff(50):
def quick_sort(line, l, r):
if r - l > 50:
pivot = find_median(line, l, r)
i, j = l+1, r-2
while True:
while line[i] < pivot:
i += 1
while line[j] > pivot:
j -= 1
if i < j:
line[i], line[j] = line[j], line[i]
i += 1
j -= 1
else:
break
line[i], line[r-1] = line[r-1], line[i]
quick_sort(line, l, i-1)
quick_sort(line, i+1, r)
else:
insert_sort_index(line, l, r)
def find_median(line, l, r):
center = (l + r) / 2
if line[l] > line[r]:
line[l], line[r] = line[r], line[l]
if line[l] > line[center]:
line[l], line[center] = line[center], line[l]
if line[center] > line[r]:
line[center], line[r] = line[r], line[center]
line[center], line[r-1] = line[r-1], line[center]
return line[r-1]
def insert_sort_index(line, l, r):
if l < r:
for idi in range(l+1, r+1):
data = line[idi]
for idj in range(idi+1)[::-1]:
if idj >= l+1 and line[idj-1] > data:
line[idj] = line[idj-1]
else:
break
line[idj] = data
The method without a cutoff:
def quick_sort(line, l, r):
if r - l > 1:
pivot = find_median(line, l, r)
i, j = l+1, r-2
while True:
while line[i] < pivot:
i += 1
while line[j] > pivot:
j -= 1
if i < j:
line[i], line[j] = line[j], line[i]
i += 1
j -= 1
else:
break
line[i], line[r-1] = line[r-1], line[i]
quick_sort(line, l, i-1)
quick_sort(line, i+1, r)
else:
if r == l + 1:
if line[l] > line[r]:
line[l], line[r] = line[r], line[l]
python3 implements range and other functions as iterators/generators, so it would probably be much more efficient in this application, but the python2 range function creates a complete list in memory. You use range multiple times insert_sort_index (and create another list with the [::-1] splice. You could have passed step as an argument to range for that one).
My python2 implementation seems to be optimizing for loops with range(0,x) which made it harder to demonstrate the problem, but not when (l, r) is within a larger list, as is the case with this quicksort cutoff.
I measured aprox. double speed of the insert sort, when operating on a range of a larger list, by using a while loop for idi, idj instead of range().

QuickSort in Python. Increment in array trouble

I am trying to implement quicksort in python. Problem is how to increment/decrement value of i/j in array a. I know that I should write i=i+1 and there are no such thing like i++ in python, but I don't understand in which way I should do this.
I am newbie, here is my code.
def quicksort(a,lo,hi):
if(hi<=lo):
return
i = lo - 1
j = hi
v = a[hi]
while True:
while(a[++i] < v):
pass
while(v < a[--j]):
if(j==lo):
break
if(i>=j):
break
t = a[i]
a[i] = a[j]
a[j] = t
t = a[i]
a[i] = a[hi]
a[hi] = t
quicksort(a, lo, i - 1)
quicksort(a, i + 1, hi)
in python, you cannot assign and get the value, that's an intentional limitation to avoid issues with typos, find the proper sequence point...
You have no choice than to "emulate" the C-ported code:
while(a[++i] < v):
pass
while(v < a[--j]):
if(j==lo):
break
(note that both constructs generate an infinite loop because:
++i == i
and
--j == j
(applying unary plus any number of times or unary minus an even number of times gives the same number, see Why Don't Two Plus Operators Throw an Error (e.g., 1 + + 2))
So change to:
i += 1
while(a[i] < v):
i += 1
j -= 1
while(v < a[j]):
if(j==lo):
break
j -= 1
The following construct does not work the same way in Python as it does in C++:
while(a[++i] < v):
as well as this one:
while(v < a[--j]):
The way you change the code is the following:
def quicksort(a,lo,hi):
if(hi<=lo):
return
i = lo - 1
j = hi
v = a[hi]
while True:
i += 1
while(a[i] < v):
i += 1
pass
j -= 1
while(v < a[j]):
j -= 1
if(j==lo):
break
if(i>=j):
break
t = a[i]
a[i] = a[j]
a[j] = t
t = a[i]
a[i] = a[hi]
a[hi] = t
quicksort(a, lo, i - 1)
quicksort(a, i + 1, hi)

Merge sort in python doesnt update the array after sorting

ab = [5, 89, 23, 9]
def mergsort(array):
mid = len(array) / 2
if mid > 0:
print (array)
mergsort(array[:mid])
mergsort(array[mid:])
print(array)
merg(array)
return array
def merg(array):
print (array)
mid = len(array)//2
left = array[:mid]
right = array[mid:]
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
array[k] = left[i]
i+=1
else:
array[k] = right[j]
j+=1
k+=1
while i < len(left):
array[k]=left[i]
i+=1
k+=1
while j < len(right):
array[k] = right[j]
j+=1
k+=1
print (array)
mergsort(ab)
print (ab)
The merge function sort the array given and the array is updated. But in the next recursion the array going into the merg function is not the mutated array.
In the example, first sorting happens and [5,89] and [23,9] are sorted as [5,89] and [9,23] but the merged input in the next recursion is [5,89,23,9] instead of [5,89,9,23].
I am unable to find any reason as mutating the array should affect the parent array.
One problem is with the recursive calls:
mergsort(array[:mid])
mergsort(array[mid:])
the results of these calls are not recorded - so when we continue, it's done with the same original unsorted array.
The fix:
def mergsort(array):
if len(array) == 1:
return array
mid=len(array)/2
left = mergsort(array[:mid]) # save into a parameter
right = mergsort(array[mid:]) # save into a parameter
return merge(left, right) # use the previous two
The second issue is actually the same kind of issue only with:
def merg(array)
the merge operation is done between two arrays, which means that two distinct arrays should be sent to this function, otherwise there is no recollection of mid from the function mergesort() and declaring mid to be length/2 treats the whole array and not the specific two parts that we intend to merge. The idea behind the logic inside this function is correct but should be done, as I mentioned, on two "distinct" arrays.
Last problem is the in-place swap which is incorrectly done, for example in:
array[k]=right[j]
by doing do, we erase the element at array[k]!
The fix:
def merge(left, right):
if len(left+right) < 2:
return left+right
res = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
res.append(left[i])
i += 1
elif j < len(right):
res.append(right[j])
j += 1
while i < len(left):
res.append(left[i])
i += 1
while j < len(right):
res.append(right[j])
j += 1
return res
After applying both fixes and running:
print mergsort(ab)
The output is:
[5, 9, 23, 89]
as required.

QuickSort Returning Recursion Depth Error

I have what should be a simple quicksort implementation, but it's returning a recursion depth exceeded error, and I'm testing it on a list of less than 30 elements. Moreover, my implementation was working on a list of 10,000 a few days ago, and the only thing I changed was moving it from a Class to a global function. Anyone see what may be causing this?
def quickSort(m, left, right):
if len(m[left:right]) <= 1:
return m
pivot = m[left]
i = left + 1
j = left + 1
for j in range(j, right):
if m[j] <= pivot:
m[j], m[i] = m[i], m[j]
i += 1
m[left], m[i-1] = m[i-1], m[left]
m = quickSort(m, left, i)
m = quickSort(m, i, right)
return m
one of your recursive calls is causing the exception(as you may have guessed :-), also note that you sort the list in place so returning the list is not necessary
def quickSort(m, left, right):
if right - left <= 1:
return
pivot = m[left]
i = left + 1
j = left + 1
for j in range(j, right):
if m[j] <= pivot:
m[j], m[i] = m[i], m[j]
i += 1
m[left], m[i-1] = m[i-1], m[left]
quickSort(m, left, i-1)
quickSort(m, i, right)

Categories

Resources