mergesort - I made 'merge sort' algorithm by using MATLAB. But not O(N*log(N)) -


i made merge sort using matlab.

and tried bar - elements' # vs elapsed time.

but graph y=x. please me tt

my code following

function result = merge_sort2(input, p, r) global a; = input; if p < r     q = floor((p+r)/2);     merge_sort2(a, p, q);     merge_sort2(a, q+1, r);     merge2(p, q, r); end result = a; end  function merge2(p, q, r) global a; n1 = q - p + 1; n2 = r - q; l = []; r = []; = 1 : n1     l(i) = a(p+i-1); end  j = 1 : n2     r(j) = a(q+j); end  l(n1+1) = inf; r(n2+1) = inf;  = 1; j = 1;  k = p : r     if l(i) <= r(j)         a(k) = l(i);         = + 1;     else         a(k) = r(j);         j = j + 1;     end end end 

and next code finding running time code.

function running_time2(func, incr, num)  y = []; max = incr * num;  = incr : incr : max     data = i:-1:1;     len = length(data);      = tic;     res = func(data, 1, len);     elapsed = toc(a);      y = [y elapsed];     fprintf('[the number of elements: %d]\t', i);     fprintf('[elapsed time: %fs]\n', elapsed); end  x = incr : incr: max; bar(x, y); end 

i typed in command window in matlab.

running_time2(@merge_sort2, 10, 300) 

the graph result this: enter image description here


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -