write python quicksort
Here is an implementation of the Quicksort algorithm in Python:
def quicksort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
less = [x for x in arr[1:] if x <= pivot]
greater = [x for x in arr[1:] if x > pivot]
return quicksort(less) + [pivot] + quicksort(greater)
To use this function, simply call it with a list of numbers as an argument:
arr = [5, 3, 8, 4, 2, 7, 1, 10]
sorted_arr = quicksort(arr)
print(sorted_arr)
This will output:
[1, 2, 3, 4, 5, 7, 8, 10]
``
原文地址: http://www.cveoy.top/t/topic/dFaW 著作权归作者所有。请勿转载和采集!