Sort array using two thirds algorithm -
i came across uncommon sorting algorithm. algorithm sorts first 2/3rd of array next 2/3rd of array , again sorts first 2/3rd of array recursively.
fun sort3(a: int list): int list = case of nil => nil | [x] => [x] | [x,y] => [int.min(x,y), int.max(x,y)] | => let val n = list.length(a) val m = (2*n+2) div 3 val res1 = sort3(list.take(a, m)) val res2 = sort3(list.drop(res1, n-m) @ list.drop(a, m)) val res3 = sort3(list.take(res1, n-m) @ list.take(res2, 2*m-n)) in res3 @ list.drop(res2,2*m-n) end
the recursive relation algorithm written
t(n) = 2t(n/3) + o(n)
i dint understand how algo works.
what understand is, 1.it recursively calls same function until array size 3 sort array using bruteforce. 2. merge arrays (like in merge sort) using o(n) time.
am right?
i dont understand language written in, not understand code.can me understanding algorithm.
ps: not homework.
Comments
Post a Comment