Sorting Output Issues, Possible Data Type Misunderstanding
Show older comments
I've written my own versions of quick, bubble, and merge sort algorithms and am testing them with a list of random numbers. If I build a list by hand, I can sort any number of variables that I wish. However, using the randi function to build a list of random integers only works with 2/3 of my sorting algorithms, basically just not with the quick sort algorithm. The output of the one in question looks something like this:
function output=quicksort(list)
%%algorithm before output
output=[sortedleftside, pivot, sortedrightside];
end
I've been able to get it to work with other random output functions such as rand, but I'm hoping to be able to build a list of random integers and compare the speed of the three sorting algorithms. Is there anything simple I'm not understanding about output in MATLAB? Perhaps the way I'm putting my outputted list is incompatible with random integers for some reason? Any help would be appreciated.
2 Comments
dpb
on 30 Nov 2013
What's the specific problem?
You've shown a function that returns a vector. Taking a guess on the problem, as written your algorithm must return a row vector irregardless of the orientation of the input argument list and I'll venture you didn't verify that sorted[left|right]side are row vectors if list is a column vector input.
Jan
on 30 Nov 2013
@Niles: Of course Matlab does not have magic problems with output of numbers created by randi. If you see, that the algorithm works with inputs created by rand, but not by randi, I guess that your sorting algorithm cannot handle inputs with equal elements.
Answers (0)
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!