I am following the Stanford course "Algorithms: Design and Analysis, Part 1", while trying implement an in place randomized selection algorithm in Python (i.e. selection based on quick sort), I believe my partition function is correct, but I just cannot figure out why the selection part keeps failing, any suggestion is greatly appreciated. My code is as follows:
import random
def random_selection(nums, start, end, i):
if end == start:
return nums[start]
elif start < end:
pivot = partition(nums, start, end)
if pivot == i:
return nums[pivot]
elif pivot < i:
# original code suffering from logic error with indices, fixed by changing 'i - pivot' into 'i'
# return random_selection(nums, pivot + 1, end, i - pivot)
return random_selection(nums, pivot + 1, end, i)
elif pivot > i:
return random_selection(nums, start, pivot - 1, i)
else:
return False
def partition(nums, start, end):
pivot_value = nums[start]
left = start + 1
right = end
done = False
while not done:
while left <= right and nums[left] < pivot_value:
left += 1
while left <= right and nums[right] > pivot_value:
right -= 1
if left > right:
done = True
else:
nums[left], nums[right] = nums[right], nums[left]
nums[start], nums[right] = nums[right], nums[start]
return right
test = range(10)
for i in range(10):
random.shuffle(test)
print random_selection(test, 0, len(test)-1, i)
Below are the results I am receiving with the test case:
0
1
None
3
4
None
5
4
8
None
The problem is you need to decide whether your indices are based on 0, or based on start.
Most of the code uses indices based on 0, except the recursive call to random_selection:
return random_selection(nums, pivot + 1, end, i - pivot)
which adjusts the i index to i - start (i.e. assuming the indices are based on start).
Changing this to:
return random_selection(nums, pivot + 1, end, i)
should give the expected results.
Related
I have an sort algorithm
def partition(competitors, left, right):
pivot = competitors[left]
i = left + 1
j = right - 1
while True:
if (i <= j and competitors[j] > pivot):
j -= 1
elif (i <= j and competitors[i] < pivot):
i += 1
elif (competitors[j] > pivot) or (competitors[i] < pivot):
continue
if i <= j:
competitors[i], competitors[j] = competitors[j], competitors[i]
else:
competitors[left], competitors[j] = competitors[j], competitors[left]
return j
def quick_sort(competitors, left, right):
if ((right - left) > 1):
p = partition(competitors, left, right)
quick_sort(competitors, left, p)
quick_sort(competitors, p + 1, right)
def transformation(competitors):
competitors[1] = - int(competitors[1])
competitors[2] = int(competitors[2])
return [competitors[1], competitors[2], competitors[0]]
if __name__ == '__main__':
number = int(input())
competitors = [transformation(input().split()) for _ in range(number)]
quick_sort(competitors, left=0, right=len(competitors))
print(*(list(zip(*competitors))[2]), sep="\n")
Here
pivot = competitors[left]
Reviewer said that i can get a pivot by divide first and last index in array.
I was try many options and one of that is
pivot = competitors[left // right]
But output is wrong
Here description of task
**
Let's get two pointers left and right, which appear on the left and right ends of the segment, respectively. Then we will move the left pointer to the right until it triggers an alarm on the element smaller than the pivot. Similarly, we move the right pointer to the left while it is on the element that exceeds the reference one.As a result, it turns out that to the left of left all elements exactly belong to the first group, and to the right of right - to the second. Elements with pointers are out of order. Let's swap them (most programming languages use the swap() function) and advance pointers to the next elements. We will repeat this action until left and right collide.
**
Input:
5
alla 4 100
gena 6 1000
gosha 2 90
rita 2 90
timofey 4 80
Output:
gena
timofey
alla
gosha
rita
And my output is: gena
timofey
alla
<rita>
<gosha>
Please help me to figure out how solve this algorithm
So this is a new algorithm, I was update it with pivot = competitors[(left + right) // 2] string
def partition(competitors, left, right):
if right <= left:
return
pivot = (left + right) // 2
part = competitors[pivot]
begin = left
end = right
while begin <= end:
while part > competitors[begin]:
begin += 1
while part < competitors[end]:
end -= 1
if begin <= end:
competitors[begin], competitors[end] = competitors[
end], competitors[begin]
begin += 1
end -= 1
partition(competitors, left, end)
partition(competitors, begin, right)
def quick_sort(competitors):
partition(competitors, 0, len(competitors) - 1)
return [line[2] for line in competitors]
if __name__ == '__main__':
n = int(input())
competitors = [None] * n
for x in range(n):
login, Pi, Fi = input().split()
competitors[x] = (-int(Pi), int(Fi), login)
result = quick_sort(competitors)
print(*result, sep="\n")
Is there a way to optimize this code?
I'm trying to find an element in the list where the sorted list is rotated. [8,9,10,11,12,6,7] Here the element I'm trying to find is 6 with an index 5.
class Solution:
def search(self, nums: List[int]) -> int:
lenn = len(nums)-1
pivot, mid = 0, (0 + lenn)//2
if(nums[0] > nums[lenn]):
while True:
if(nums[mid] > nums[mid+1]):
pivot = mid+1
break
if(nums[mid] < nums[mid-1]):
pivot = mid
break
if(nums[mid] > nums[lenn]):
mid += 1
if(nums[mid] < nums[lenn]):
mid -= 1
return pivot
Your solution is fast for small lists but not for big ones as it perform a linear-time search. To be faster, you can use a binary search. To do that, you need to use a keep a [start;end] range of values that contains the "pivot". You just need to take the middle item and find which part (left or right) contains increasing items.
Moreover, you can micro-optimize the code. The default implementation of Python is CPython which is a slow interpreter. It optimize almost nothing compared to compilers (of languages like C, Java, Rust) so you should do the optimizations yourself if you want a fast code. The idea is to use a linear search for few items and a binary search for many ones, and to store values in temporary variables not to compute them twice.
Here is the resulting implementation:
def search(nums):
first, last = nums[0], nums[-1]
# Trivial case
if first <= last:
return 0
lenn = len(nums)-1
# Micro-optimized fast linear search for few items (optional)
if lenn < 10:
pivot, mid = 0, lenn//2
while True:
middle = nums[mid]
if middle > nums[mid+1]:
return mid+1
elif middle < nums[mid-1]:
return mid
elif middle > last:
mid += 1
elif middle < last:
mid -= 1
# Binary search for many items
start, end = 0, lenn
while start+1 < end:
mid = (start + end) // 2
midVal = nums[mid]
if midVal < nums[start]:
end = mid
elif midVal > nums[end]:
start = mid
else:
assert False
return start+1
I am trying to implement the binary search in python and have written it as follows. However, I can't make it stop whenever needle_element is larger than the largest element in the array.
Can you help? Thanks.
def binary_search(array, needle_element):
mid = (len(array)) / 2
if not len(array):
raise "Error"
if needle_element == array[mid]:
return mid
elif needle_element > array[mid]:
return mid + binary_search(array[mid:],needle_element)
elif needle_element < array[mid]:
return binary_search(array[:mid],needle_element)
else:
raise "Error"
It would be much better to work with a lower and upper indexes as Lasse V. Karlsen was suggesting in a comment to the question.
This would be the code:
def binary_search(array, target):
lower = 0
upper = len(array)
while lower < upper: # use < instead of <=
x = lower + (upper - lower) // 2
val = array[x]
if target == val:
return x
elif target > val:
if lower == x: # these two are the actual lines
break # you're looking for
lower = x
elif target < val:
upper = x
lower < upper will stop once you have reached the smaller number (from the left side)
if lower == x: break will stop once you've reached the higher number (from the right side)
Example:
>>> binary_search([1,5,8,10], 5) # return 1
1
>>> binary_search([1,5,8,10], 0) # return None
>>> binary_search([1,5,8,10], 15) # return None
Why not use the bisect module? It should do the job you need---less code for you to maintain and test.
array[mid:] creates a new sub-copy everytime you call it = slow. Also you use recursion, which in Python is slow, too.
Try this:
def binarysearch(sequence, value):
lo, hi = 0, len(sequence) - 1
while lo <= hi:
mid = (lo + hi) // 2
if sequence[mid] < value:
lo = mid + 1
elif value < sequence[mid]:
hi = mid - 1
else:
return mid
return None
In the case that needle_element > array[mid], you currently pass array[mid:] to the recursive call. But you know that array[mid] is too small, so you can pass array[mid+1:] instead (and adjust the returned index accordingly).
If the needle is larger than all the elements in the array, doing it this way will eventually give you an empty array, and an error will be raised as expected.
Note: Creating a sub-array each time will result in bad performance for large arrays. It's better to pass in the bounds of the array instead.
You can improve your algorithm as the others suggested, but let's first look at why it doesn't work:
You're getting stuck in a loop because if needle_element > array[mid], you're including element mid in the bisected array you search next. So if needle is not in the array, you'll eventually be searching an array of length one forever. Pass array[mid+1:] instead (it's legal even if mid+1 is not a valid index), and you'll eventually call your function with an array of length zero. So len(array) == 0 means "not found", not an error. Handle it appropriately.
This is a tail recursive solution, I think this is cleaner than copying partial arrays and then keeping track of the indexes for returning:
def binarySearch(elem, arr):
# return the index at which elem lies, or return false
# if elem is not found
# pre: array must be sorted
return binarySearchHelper(elem, arr, 0, len(arr) - 1)
def binarySearchHelper(elem, arr, start, end):
if start > end:
return False
mid = (start + end)//2
if arr[mid] == elem:
return mid
elif arr[mid] > elem:
# recurse to the left of mid
return binarySearchHelper(elem, arr, start, mid - 1)
else:
# recurse to the right of mid
return binarySearchHelper(elem, arr, mid + 1, end)
def binary_search(array, target):
low = 0
mid = len(array) / 2
upper = len(array)
if len(array) == 1:
if array[0] == target:
print target
return array[0]
else:
return False
if target == array[mid]:
print array[mid]
return mid
else:
if mid > low:
arrayl = array[0:mid]
binary_search(arrayl, target)
if upper > mid:
arrayu = array[mid:len(array)]
binary_search(arrayu, target)
if __name__ == "__main__":
a = [3,2,9,8,4,1,9,6,5,9,7]
binary_search(a,9)
Using Recursion:
def binarySearch(arr,item):
c = len(arr)//2
if item > arr[c]:
ans = binarySearch(arr[c+1:],item)
if ans:
return binarySearch(arr[c+1],item)+c+1
elif item < arr[c]:
return binarySearch(arr[:c],item)
else:
return c
binarySearch([1,5,8,10,20,50,60],10)
All the answers above are true , but I think it would help to share my code
def binary_search(number):
numbers_list = range(20, 100)
i = 0
j = len(numbers_list)
while i < j:
middle = int((i + j) / 2)
if number > numbers_list[middle]:
i = middle + 1
else:
j = middle
return 'the index is '+str(i)
If you're doing a binary search, I'm guessing the array is sorted. If that is true you should be able to compare the last element in the array to the needle_element. As octopus says, this can be done before the search begins.
You can just check to see that needle_element is in the bounds of the array before starting at all. This will make it more efficient also, since you won't have to do several steps to get to the end.
if needle_element < array[0] or needle_element > array[-1]:
# do something, raise error perhaps?
It returns the index of key in array by using recursive.
round() is a function convert float to integer and make code fast and goes to expected case[O(logn)].
A=[1,2,3,4,5,6,7,8,9,10]
low = 0
hi = len(A)
v=3
def BS(A,low,hi,v):
mid = round((hi+low)/2.0)
if v == mid:
print ("You have found dude!" + " " + "Index of v is ", A.index(v))
elif v < mid:
print ("Item is smaller than mid")
hi = mid-1
BS(A,low,hi,v)
else :
print ("Item is greater than mid")
low = mid + 1
BS(A,low,hi,v)
BS(A,low,hi,v)
Without the lower/upper indexes this should also do:
def exists_element(element, array):
if not array:
yield False
mid = len(array) // 2
if element == array[mid]:
yield True
elif element < array[mid]:
yield from exists_element(element, array[:mid])
else:
yield from exists_element(element, array[mid + 1:])
Returning a boolean if the value is in the list.
Capture the first and last index of the list, loop and divide the list capturing the mid value.
In each loop will do the same, then compare if value input is equal to mid value.
def binarySearch(array, value):
array = sorted(array)
first = 0
last = len(array) - 1
while first <= last:
midIndex = (first + last) // 2
midValue = array[midIndex]
if value == midValue:
return True
if value < midValue:
last = midIndex - 1
if value > midValue:
first = midIndex + 1
return False
I'm having trouble getting the right output on my "Majority Element" divide and conquer algorithm implementation in Python 3.
This should be relatively correct; however, it would still appear that I'm missing something or it is slightly off and I cannot figure out why that is.
I've tried some debugging statement and different things. It looks like on the particular case listed in the comment above the code, it's resolving -1 for "left_m" and 941795895 for "right_m" when it does the recursive calls. When it compares the element at each index to those variables, the counter will obviously never increment.
Am I going about this the wrong way? Any help would be greatly appreciated.
Thanks.
# Input:
# 10
# 2 124554847 2 941795895 2 2 2 2 792755190 756617003
# Your output:
# 0
#
# Correct output:
# 1
def get_majority_element(a, left, right):
if left == right:
return -1
if left + 1 == right:
return a[left]
left_m = get_majority_element(a, left, (left + right - 1)//2)
right_m = get_majority_element(a, (left + right - 1)//2 + 1, right)
left_count = 0
for i in range(0, right):
if a[i] == left_m:
left_count += 1
if left_count > len(a)//2:
return left_m
right_count = 0
for i in range(0, right):
if a[i] == right_m:
right_count += 1
if right_count > len(a)//2:
return right_m
return -1
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
if get_majority_element(a, 0, n) != -1:
print(1)
else:
print(0)
When counting the appearance of left and right major elements, your loops go over the range(0, right). Instead, they should be going over the range(left, right). Starting from 0 may, and will cause returning incorrect major elements in the smaller subproblems.
Also, in addition to the problem of having incorrect starting indices in ranges your for loops cover, you seem to have a problem in your recursive call arguments, probably due to your intuition causing you to overlook some details. Throughout the get_majority_element function, you treat parameter right to be the first index that is not in the list, as opposed to right being the index of the rightmost element in the list.
However, in your first recursive call, you give the third argument as if it is the last element included in that list. If right is the index of the first element not in that list, it should actually be the same with the second parameter of the second recursive call you're making in the following line. So, the third argument of the first recursive call you're making is less than it should be, by 1, causing you to overlook 1 element each time you recursively go down.
Thirdly, you have an error in the if statements following the for loops, similar to the problem you had with loop ranges. You are dividing the occurances of the element for all len(a) elements, although you should only care about the subproblem you are currently working on. So, you should divide it by the number of elements in that subproblem, rather than len(a). (i.e. (right - left)//2)
You can find the working code below, and look here in order to observe it's execution.
def get_majority_element(a, left, right):
if left == right:
return -1
if left + 1 == right:
return a[left]
left_m = get_majority_element(a, left, (left + right - 1)//2 + 1)
right_m = get_majority_element(a, (left + right - 1)//2 + 1, right)
left_count = 0
for i in range(left, right):
if a[i] == left_m:
left_count += 1
if left_count > (right-left)//2:
return left_m
right_count = 0
for i in range(left, right):
if a[i] == right_m:
right_count += 1
if right_count > (right-left)//2:
return right_m
return -1
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
print("n=" + str(n))
if get_majority_element(a, 0, len(a)) != -1:
print(1)
else:
print(0)
I am trying to use Boyers and Moore's algorithm to find the majority element among a list. I am using a inbuilt function, count; so, if the majority element is greater than half size of the list then it gives output of 1, otherwise 0. You can find more in this link about Boyers and Moore's Algorithm on finding majority Algorithm information here
# Uses python3
import sys
def get_majority_element(a,n):
maximum = a[0]
amount = 1
for i in (a[1:]):
if not maximum == i:
if amount >= 1:
amount = amount - 1
else:
maximum = i
amount = 1
else:
amount = amount + 1
output = a.count(maximum)
if output > n//2:
return 1
return 0
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
print (get_majority_element(a,n))
I was trying to implement the algorithm discussed in this video and also in this document.
My quicksort code, which depends on picking the middle element in the array as the pivot (see below), as opposed to the approach used by the author of the document above, which uses the first element in the array as the pivot as shown here in the video.
Obviously, my code doesn't work (runs out of the recursion limit eventually). I wonder if it's because of some silly error in my code or that it simply would NOT work as long as I pick the pivot from the middle.
def partition(a, start, end):
# I pick the pivot as the middle item in the array
# but the algorithm shown in the video seems to
# pick the first element in the array as pivot
piv = (start + end) // 2
pivotVal = a[piv]
left = start
right = end
while left <= right:
while a[left] < pivotVal:
left += 1
while a[right] > pivotVal:
right -= 1
if left <= right:
temp = a[left]
a[left] = a[right]
a[right] = temp
left += 1
right -= 1
return left
def quicksort(a, start, end, k):
if start < end:
piv = partition(a, start, end)
if piv == (k - 1):
print("Found kth smallest: " + piv)
return a[piv]
elif piv > (k - 1):
return quicksort(a, start, piv, k)
else:
return quicksort(a, piv + 1, end, k)
myList = [54, 26, 93, 17, 77, 31, 44, 55, 20]
quicksort(myList, 0, len(myList) - 1, 3)
print(myList)
If you are using inclusive array bounds, which isn't the most convenient trick, you'll have to change the range in the recursive calls to [start, piv - 1] and [piv + 1, end]
Reason being, you are reconsidering the piv element in the each recursion that goes to the left of array.
The code with said changes ran without any stack overflow error.
EDIT The output isn't right for few values of k. You may need to check your partition logic again.