function partition(numbers: array, low: integer, high: integer)
i: integer = low
j: integer = high
tmp: integer
pivot: integer = numbers[(low + high) / 2]
loop(while i <= j)
loop(while numbers[i] < pivot)
i++
end loop
loop(while numbers[j] > pivot)
j--
end loop
if(i <= j)
tmp = numbers[i]
numbers[i] = numbers[j]
numbers[j] = tmp
i++
j--
end if
end loop
return i
end function
function sort(numbers: array, low: integer, high: integer)
index: integer = partition(numbers, low, high)
if(low < index - 1)
sort(numbers, low, index - 1)
end if
if(index < high)
sort(numbers, index, high)
end if
end function
partition这个方法是干嘛的?
这个算法的时间复杂度是多少?
如果没有排序,而是随机的顺序,那时间复杂度是多少?
这是一个最简单的快速排序程序。
你先去了解下什么是快速排序,自然就理解了。
最佳时间复杂度 -
nlog(n)
;最差时间复杂度 -n*n
。详见 https://subetter.com/algorith...一个算法的时间复杂度我们一般有三个指标,最差、最佳、平均。前两个已经说过了,第三个解释起来很难,我也没本事解释,因为这确实很难很难很难!但可以告诉你答案,因为大牛已经解释过了,是
nlog(n)
。