Merge Sort algorithm solved using recursive MATLAB Function

This video shows how to solve the Merge Sort Algorithm in MATLAB recursively.

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code

function y = MergeSort(x)

n = length(x);

if(n less_than_sign 2)
% Return Condition
y=x;
return;
end

n_2 = int32(n/2);

x1=MergeSort(x(1:n_2));
x2=MergeSort(x(n_2+1:n));

n1 = length(x1);
n2 = length(x2);

count_x1 = 1;
count_x2 = 1;

y = [];

while(count_x1less_than_equal_to_sign n1)

if(count_x2 greater_than_sign n2)
y = [y x1(count_x1)];
count_x1 = count_x1+1;
continue;
end

if(x1(count_x1) less_than_sign x2(count_x2))
y = [y x1(count_x1)];
count_x1 = count_x1+1;
else
y = [y x2(count_x2)];
count_x2 = count_x2+1;
end

end

for j=count_x2:n2
y = [y x2(j)];
end
end

Leave a Reply