Loading AI tools
来自维基百科,自由的百科全书
臭皮匠排序(英语:Stooge Sort)是一种采用分治法的低效排序算法,甚至慢于冒泡排序。在《算法导论》第二版第7章(快速排序)的思考题中被提到,是由Howard、Fine等教授提出的所谓“漂亮的”排序算法。
algorithm stoogesort(array L, i = 0, j = length(L)-1)
if L[j] < L[i] then
L[i] ↔ L[j]
if (j - i + 1) >= 3 then
t = (j - i + 1) / 3
stoogesort(L, i , j-t)
stoogesort(L, i+t, j )
stoogesort(L, i , j-t)
return L
# Julia Sample : Stooge Sort
function StoogeSort(A,v1,v2)
if A[v1]>A[v2]
A[v1],A[v2] = A[v2],A[v1]
end
if (v2-v1+1)>2
t = Int(round((v2-v1+1)/3))
StoogeSort(A, v1 , v2-t)
StoogeSort(A, v1+t, v2 )
StoogeSort(A, v1 , v2-t)
end
return A
end
# Main Code
A = [16,586,1,31,354,43,3]
println(A) # Original Array
println(StoogeSort(A,1,length(A))) # Stooge Sort Array
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.