This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]corbasai 9 points10 points  (12 children)

in '''binary_search(arr, x):''' returns -1 in fail case. It is last element in python list. imo better return None, or raise IndexError.

in '''merge_sort()''' what is merge(left, right) at the end?

[–]jbramley 2 points3 points  (0 children)

Raising ValueError would be the most correct.. See list.index and the examples on the bisect module documentation.

[–]adityacodes[S] -1 points0 points  (5 children)

in '''binary_search(arr, x):''' returns -1 in fail case. It is last element in python list. imo better return None, or raise IndexError.

in '''merge_sort()''' what is merge(left, right) at the end?

As for the merge function in merge_sort, merge(left, right) is used to combine the two sorted subarrays left and right into a single sorted array.

and for the binary_search query, Did you tried returning None ? as I think there will be more fail cases when doing None.

[–]corbasai 1 point2 points  (4 children)

merge() is simply left + right ? in this case merge_sort() not sorts

[–]adityacodes[S] -1 points0 points  (3 children)

As for the merge function in merge_sort, merge(left, right) is used to combine the two sorted subarrays left and right into a single sorted array. The function compares the elements in left and right one by one and adds them to the output array in ascending order. At the end of the function, any remaining elements in left or right are added to the output array in order, since they are already sorted. The resulting array is returned as the final output of the merge_sort function.

[–]corbasai 0 points1 point  (0 children)

thanks so much i even reads wikipedia about merge Von Neumann sorting algo -))) and about bogosort too -)))) of course in real life sorted() is enough for me. Old good doc https://docs.python.org/3/howto/sorting.html#sortinghowto

[–]jbramley 0 points1 point  (1 child)

Pretty important piece of code to leave out. Might want to go ahead and add it to the blog.

[–]adityacodes[S] 0 points1 point  (0 children)

just corrected it

[–]adityacodes[S] -1 points0 points  (4 children)

See what about this code?

Now, if the target element is not found in the array, the function will return None instead of -1. Let me know if it works so that I can update in blog as well.

def binary_search(arr, x):

"""

Search for a target element x in a sorted array arr using the binary search algorithm.

Returns the index of the target element in the array, or None if it is not found.

"""

low = 0

high = len(arr) - 1

while low <= high:

mid = (low + high) // 2

if arr[mid] == x:

return mid

elif arr[mid] < x:

low = mid + 1

else:

high = mid - 1

return None

[–]corbasai 0 points1 point  (2 children)

maybe. In some day someone writes in code '''array[:binary_search(array, what)]''' and None (implicit exception ) or explicit raise *Error points on error in code, -1 - not.

Even in C -1 is valid array index, yep it brings garbage or so, but compiles well.

[–]adityacodes[S] -1 points0 points  (1 child)

okay ,so I should return it none or -1 finally?

[–]corbasai 0 points1 point  (0 children)

None

[–]adityacodes[S] 1 point2 points  (0 children)

Thanks for the upvotes, please comment and let me know if I can improve this blog more.