I tried several variations of quick sorting for an array of random numbers.
I declare an array:
n = 10 my_array = [] for i in range(n): my_array.append(randint(1, 2000))
The first sorting variation:
def my_quick(array): chosen = array[0] low = [elem for elem in array[1:] if elem <= chosen] high = [elem for elem in array[1:] if elem > chosen] return my_quick(low) + [chosen] + my_quick(high)
Second:
def my_quick(array): low = [] middle = [] high = [] chosen = my_array[0] for elem in array: if elem < chosen: low.append(elem) elif elem > chosen: high.append(elem) else: middle.append(elem) return my_quick(low)+middle+my_quick(high)
Errors (same for 2 implementations): 1. When calling a function. 2. 3 times displays an error in the last line of sorts. 3. array [0] (IndexError: list index out of range)
Though I do not understand what is the matter and how to fix it, in many sources the similar code