mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67. fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left]) update 'sorted' to 'ascending sorted' in description to avoid confusion * delete swap files * delete 'address' and add input validation
This commit is contained in:
@ -14,9 +14,9 @@ def _partition(data, pivot):
|
||||
"""
|
||||
less, equal, greater = [], [], []
|
||||
for element in data:
|
||||
if element.address < pivot.address:
|
||||
if element < pivot:
|
||||
less.append(element)
|
||||
elif element.address > pivot.address:
|
||||
elif element > pivot:
|
||||
greater.append(element)
|
||||
else:
|
||||
equal.append(element)
|
||||
@ -24,6 +24,11 @@ def _partition(data, pivot):
|
||||
|
||||
def quickSelect(list, k):
|
||||
#k = len(list) // 2 when trying to find the median (index that value would be when list is sorted)
|
||||
|
||||
#invalid input
|
||||
if k>=len(list) or k<0:
|
||||
return None
|
||||
|
||||
smaller = []
|
||||
larger = []
|
||||
pivot = random.randint(0, len(list) - 1)
|
||||
@ -41,4 +46,4 @@ def quickSelect(list, k):
|
||||
return quickSelect(smaller, k)
|
||||
#must be in larger
|
||||
else:
|
||||
return quickSelect(larger, k - (m + count))
|
||||
return quickSelect(larger, k - (m + count))
|
Reference in New Issue
Block a user