performance - Why does the sorting algorithm work correctly? -


i got sorting algorithm. know have been written in many other , more simple ways that's not point of question.

here the algorithm:

sort(a : array of n, : n, j : n) assert j-i+1 istwopotency if a[i] > a[j] swap a[i] , a[j] if i+1 < j     k:= (j − + 1)/4     sort(a, i, j − 2k)     sort(a, j − 2k + 1, j)     sort(a, + k, j − k)     sort(a, i, j − 2k)     sort(a, j − 2k + 1, j)     sort(a, + k, j − k) 

my question is, why algorithm work correctly in following case?

sort(a, 1, length(a)) 

and array be:

a[1 . . . length(a)] 

length(a) 2 potency , can assume there no identical numbers inside array. tested it, got no errors assume works correctly. how can prove algorithm works correctly in these conditions?

and wondering how long algorithm needs running time. great if give me running time big theta notation (that's 1 understand best)

f(n) = Θ(g(n))

correctness

divide array 4 quarters, a1 through a4, , consider sub-array each element should end in.

  • after first 2 recursive calls, elements belong in a1 located in a1 or a3. a4 elements located in a2 or a4.

  • after third recursive call, a1 elements in a1 or a2, , a4 elements in a3 or a4.

  • after next 2 recursive calls, a1 elements in a1 in sorted order, , a4 elements in a4 in sorted order. leaves a2 , a3 elements in either a2 or a3.

  • after last recursive call a2 , a3 elements in correct sub-array in sorted order. array sorted.

illustration:

schematic illustration of above explanation

runtime

notice when execute algorithm on array of length n, have execute algorithm 6 times on arrays of length n/2. yields following recurrence:

  • t(1) = o(1).
  • t(n) = 6 t(n/2) + o(1).

solving recurrence t(n) = o(6^log2(n)) = o(n^log2(6)) ≈ o(n^2.585)


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -