def quicksort(x): | |
if len(x) == 1 or len(x) == 0: | |
return x | |
else: | |
pivot = x[0] | |
i = 0 | |
for j in range(len(x)-1): | |
if x[j+1] < pivot: | |
x[j+1],x[i+1] = x[i+1], x[j+1] | |
i += 1 | |
x[0],x[i] = x[i],x[0] | |
first_part = quicksort(x[:i]) | |
second_part = quicksort(x[i+1:]) | |
first_part.append(x[i]) | |
return first_part + second_part | |
alist = [54,26,93,17,77,31,44,55,20] | |
quicksort(alist) | |
print(alist) |
This comment has been minimized.
This comment has been minimized.
And this is Lomuto way :) |
This comment has been minimized.
This comment has been minimized.
Doesn't work. |
This comment has been minimized.
This comment has been minimized.
I fixed it.
|
This comment has been minimized.
This comment has been minimized.
I think that you could re-write to something like this also: def quicksort(x):
if len(x) < 2:
return x
else:
pivot = x[0]
less = [i for i in x[1:] if i <= pivot]
greater = [i for i in x[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greater) |
This comment has been minimized.
This comment has been minimized.
How can I change this to a reverse quicksort? |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
def reverse_quicksort(x): |
This comment has been minimized.
This comment has been minimized.
doesn,t work .code has some error. |
This comment has been minimized.
This comment has been minimized.
how to convert this to randomized quickSort |
This comment has been minimized.
This comment has been minimized.
data = [ int(random.random() * 1000) for i in range(1000000)] This code will break down with larger ranges like above. but it is quick however merge sort is quicker quick sort of 100000 numbers is 0.981563091278 seconds and will not break down for comparison here is bubble sort; and I got tired of waiting on it so range is lower. |
This comment has been minimized.
This comment has been minimized.
I think you are 2 times traversing the list. |
This comment has been minimized.
This comment has been minimized.
quicksort([8, 3, 1, 7, 0, 10, 2]) and it doesn't work |
This comment has been minimized.
This comment has been minimized.
Bueno pues la razón de que no funcione es que no estamos teniendo en cuenta el tipo de dato que comparamos:
|
This comment has been minimized.
This comment has been minimized.
simple method a= [4,7,1,2,3,9,7,0,4,56,3] |
This comment has been minimized.
This comment has been minimized.
@pete312 use pypy3 instead of python to see some BIG RUNNING TIME DIFFRENCE |
This comment has been minimized.
Some bugs.
Replace quickSort with quicksort in 18 line.