Merge sort algorithm to solve different problem. Code quality? [on hold]












-2












$begingroup$


I am a beginner in Python and wrote this program to solve the following question. where are the areas that I can improve the code? I want to write in Pythonic Way. Any advice on how to improve my code? I am from a C background so python is a bit different in terms of syntax.



Q- I have an array A with the sizes of apples and have to create another array S which would contain the indices of the apples in sorted order given that we cannot directly access or touch A only a function is_large(A,i,j) function can access it. It returns -1 is A[i] > A[j] and 1 if A[i] < A[j]. I wrote the program but it is giving incorrect results even for small arrays what is the problem? The main problem is that the first element is not changing positions because of that the whole array is unsorted.



def is_large_apples(apple_size, i, j):
""" Takes two indices and tells
which one of them is larger or smaller """

if apple_size[i] > apple_size[j]:
return 1
elif apple_size[i] < apple_size[j]:
return -1

def mergesort_apples(s, l, r):
""" This function takes indexed list and
makes recursive calls to sort the array """
if l < r:
mid = (l+r)//2
mergesort_apples(s, l, mid)
mergesort_apples(s, mid+1, r)
merge_apples(s, l, mid, r)


def merge_apples(s, l, mid, r):
""" This function takes the list and
the indices to merge them into the final array"""
nl = mid - l + 1
nr = r - mid
left, right = ,
left = s[l:mid+1:1]
right = s[mid+1:r+1:1]
i, j, k = 0, 0, l;
while i < nl and j < nr:
print(s)
if is_large_apples(apple_size, left[i], right[j]) == -1:
s[k] = left[i]
i += 1
else:
s[k] = right[j]
j += 1
k += 1

while i < nl:
s[k] = left[i]
k += 1
i += 1
while j < nr:
s[k] = right[j]
k += 1
j += 1

apple_size = [5, 7, 1,44,2,33] # Given list of sizes.
s = [x for x in range(0,len(apple_size))] # Original list of indices.
mergesort_apples(s, 0, len(s)-1)
print(s)









share|improve this question











$endgroup$



put on hold as off-topic by πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    Hey, welcome to Code Review! Here we only review code that works as intended, broken code is off-topic. Once your code does what you want we would love to help you achieve the same thing in a better way! For more information have a look at our help center.
    $endgroup$
    – Graipher
    16 hours ago
















-2












$begingroup$


I am a beginner in Python and wrote this program to solve the following question. where are the areas that I can improve the code? I want to write in Pythonic Way. Any advice on how to improve my code? I am from a C background so python is a bit different in terms of syntax.



Q- I have an array A with the sizes of apples and have to create another array S which would contain the indices of the apples in sorted order given that we cannot directly access or touch A only a function is_large(A,i,j) function can access it. It returns -1 is A[i] > A[j] and 1 if A[i] < A[j]. I wrote the program but it is giving incorrect results even for small arrays what is the problem? The main problem is that the first element is not changing positions because of that the whole array is unsorted.



def is_large_apples(apple_size, i, j):
""" Takes two indices and tells
which one of them is larger or smaller """

if apple_size[i] > apple_size[j]:
return 1
elif apple_size[i] < apple_size[j]:
return -1

def mergesort_apples(s, l, r):
""" This function takes indexed list and
makes recursive calls to sort the array """
if l < r:
mid = (l+r)//2
mergesort_apples(s, l, mid)
mergesort_apples(s, mid+1, r)
merge_apples(s, l, mid, r)


def merge_apples(s, l, mid, r):
""" This function takes the list and
the indices to merge them into the final array"""
nl = mid - l + 1
nr = r - mid
left, right = ,
left = s[l:mid+1:1]
right = s[mid+1:r+1:1]
i, j, k = 0, 0, l;
while i < nl and j < nr:
print(s)
if is_large_apples(apple_size, left[i], right[j]) == -1:
s[k] = left[i]
i += 1
else:
s[k] = right[j]
j += 1
k += 1

while i < nl:
s[k] = left[i]
k += 1
i += 1
while j < nr:
s[k] = right[j]
k += 1
j += 1

apple_size = [5, 7, 1,44,2,33] # Given list of sizes.
s = [x for x in range(0,len(apple_size))] # Original list of indices.
mergesort_apples(s, 0, len(s)-1)
print(s)









share|improve this question











$endgroup$



put on hold as off-topic by πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    Hey, welcome to Code Review! Here we only review code that works as intended, broken code is off-topic. Once your code does what you want we would love to help you achieve the same thing in a better way! For more information have a look at our help center.
    $endgroup$
    – Graipher
    16 hours ago














-2












-2








-2





$begingroup$


I am a beginner in Python and wrote this program to solve the following question. where are the areas that I can improve the code? I want to write in Pythonic Way. Any advice on how to improve my code? I am from a C background so python is a bit different in terms of syntax.



Q- I have an array A with the sizes of apples and have to create another array S which would contain the indices of the apples in sorted order given that we cannot directly access or touch A only a function is_large(A,i,j) function can access it. It returns -1 is A[i] > A[j] and 1 if A[i] < A[j]. I wrote the program but it is giving incorrect results even for small arrays what is the problem? The main problem is that the first element is not changing positions because of that the whole array is unsorted.



def is_large_apples(apple_size, i, j):
""" Takes two indices and tells
which one of them is larger or smaller """

if apple_size[i] > apple_size[j]:
return 1
elif apple_size[i] < apple_size[j]:
return -1

def mergesort_apples(s, l, r):
""" This function takes indexed list and
makes recursive calls to sort the array """
if l < r:
mid = (l+r)//2
mergesort_apples(s, l, mid)
mergesort_apples(s, mid+1, r)
merge_apples(s, l, mid, r)


def merge_apples(s, l, mid, r):
""" This function takes the list and
the indices to merge them into the final array"""
nl = mid - l + 1
nr = r - mid
left, right = ,
left = s[l:mid+1:1]
right = s[mid+1:r+1:1]
i, j, k = 0, 0, l;
while i < nl and j < nr:
print(s)
if is_large_apples(apple_size, left[i], right[j]) == -1:
s[k] = left[i]
i += 1
else:
s[k] = right[j]
j += 1
k += 1

while i < nl:
s[k] = left[i]
k += 1
i += 1
while j < nr:
s[k] = right[j]
k += 1
j += 1

apple_size = [5, 7, 1,44,2,33] # Given list of sizes.
s = [x for x in range(0,len(apple_size))] # Original list of indices.
mergesort_apples(s, 0, len(s)-1)
print(s)









share|improve this question











$endgroup$




I am a beginner in Python and wrote this program to solve the following question. where are the areas that I can improve the code? I want to write in Pythonic Way. Any advice on how to improve my code? I am from a C background so python is a bit different in terms of syntax.



Q- I have an array A with the sizes of apples and have to create another array S which would contain the indices of the apples in sorted order given that we cannot directly access or touch A only a function is_large(A,i,j) function can access it. It returns -1 is A[i] > A[j] and 1 if A[i] < A[j]. I wrote the program but it is giving incorrect results even for small arrays what is the problem? The main problem is that the first element is not changing positions because of that the whole array is unsorted.



def is_large_apples(apple_size, i, j):
""" Takes two indices and tells
which one of them is larger or smaller """

if apple_size[i] > apple_size[j]:
return 1
elif apple_size[i] < apple_size[j]:
return -1

def mergesort_apples(s, l, r):
""" This function takes indexed list and
makes recursive calls to sort the array """
if l < r:
mid = (l+r)//2
mergesort_apples(s, l, mid)
mergesort_apples(s, mid+1, r)
merge_apples(s, l, mid, r)


def merge_apples(s, l, mid, r):
""" This function takes the list and
the indices to merge them into the final array"""
nl = mid - l + 1
nr = r - mid
left, right = ,
left = s[l:mid+1:1]
right = s[mid+1:r+1:1]
i, j, k = 0, 0, l;
while i < nl and j < nr:
print(s)
if is_large_apples(apple_size, left[i], right[j]) == -1:
s[k] = left[i]
i += 1
else:
s[k] = right[j]
j += 1
k += 1

while i < nl:
s[k] = left[i]
k += 1
i += 1
while j < nr:
s[k] = right[j]
k += 1
j += 1

apple_size = [5, 7, 1,44,2,33] # Given list of sizes.
s = [x for x in range(0,len(apple_size))] # Original list of indices.
mergesort_apples(s, 0, len(s)-1)
print(s)






python algorithm mergesort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 mins ago







dsaharia

















asked 19 hours ago









dsahariadsaharia

72




72




put on hold as off-topic by πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.







put on hold as off-topic by πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal 4 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Ludisposed, 200_success, Graipher, Jamal

If this question can be reworded to fit the rules in the help center, please edit the question.












  • $begingroup$
    Hey, welcome to Code Review! Here we only review code that works as intended, broken code is off-topic. Once your code does what you want we would love to help you achieve the same thing in a better way! For more information have a look at our help center.
    $endgroup$
    – Graipher
    16 hours ago


















  • $begingroup$
    Hey, welcome to Code Review! Here we only review code that works as intended, broken code is off-topic. Once your code does what you want we would love to help you achieve the same thing in a better way! For more information have a look at our help center.
    $endgroup$
    – Graipher
    16 hours ago
















$begingroup$
Hey, welcome to Code Review! Here we only review code that works as intended, broken code is off-topic. Once your code does what you want we would love to help you achieve the same thing in a better way! For more information have a look at our help center.
$endgroup$
– Graipher
16 hours ago




$begingroup$
Hey, welcome to Code Review! Here we only review code that works as intended, broken code is off-topic. Once your code does what you want we would love to help you achieve the same thing in a better way! For more information have a look at our help center.
$endgroup$
– Graipher
16 hours ago










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin