User-Defined Selection Sort for 2-D arrays of any size (MATLAB)
Show older comments
I am trying to create a user-defined selection sort for 2-D arrays. The user will select column to sort.
However, as the desired column is selected all columns are to be moved at the same time so that the values in a single row are kept together.
My attempt:
load sort_data.dat
col = input('Please enter the column you wish to sort: ');
sortorder(sort_data,col)
-------------------------------------------------------------------------------------
function [value,loc] = sortorder(array,col) %This function uses the selection sort method to sort a column of an array
[n,m] = size(array); loc = 1; value = 1; i = n;
j = 1;
for i = 1:n
i = i - 1 ;
if i >= 2
value = max(array(1:i,col));
else
array(1:n,1:m);
if [value] ~= max(array(i, col))
for j = 1:m
j = j + 1;
temp = array(loc,j);
array(loc,j) = array(i,j);
array(i,j) = temp;
end
end
end
end
Any ideas?
1 Comment
the cyclist
on 1 Nov 2014
What's your question? Does this code give an error? Not work as expected? Work as expected but slow?
Accepted Answer
More Answers (3)
the cyclist
on 1 Nov 2014
If col is the desire sorting order, like [1 3 4 2], then why not just
sorted_array = array(:,col)
?
6 Comments
Anthony
on 1 Nov 2014
Anthony
on 1 Nov 2014
James Tursa
on 1 Nov 2014
Did you try cyclist's solution?
Anthony
on 1 Nov 2014
James Tursa
on 1 Nov 2014
Edited: James Tursa
on 1 Nov 2014
Probably an unfortunate choice of variable names confusion issue. Your col variable is a single value indicating the column to sort on (an input), whereas cyclist's col variable is a vector of sorted indexes (i.e., an output of sorting). Think of cyclist's col variable as your loc variable ... the original row indexes of the column in sorted order. (At least that's what if looks like your loc variable is supposed to be ... I haven't gone through your code in detail).
Roger Stafford
on 1 Nov 2014
I think there is a misunderstanding here about Anthony's 'col' input. I believe he intended for it to be a scalar column index number, not a permutation of a column. His words, "The user will select column to sort" indicate that.
the cyclist
on 1 Nov 2014
Edited: the cyclist
on 1 Nov 2014
I realize I am somewhat confused by what you want to do. Now I am wondering if what you actually want is the sortrows command. That will allow you to select a column (or columns), and MATLAB will then sort the entire array (keeping rows intact) according to that column.
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> A_sorted_by_third_column = sortrows(A,3)
A_sorted_by_third_column =
4 9 2
8 1 6
3 5 7
Image Analyst
on 1 Nov 2014
That's way, way too complicated. If you want a wrapper around the built-in function that does that, then just simply do this:
function [sortedArray, sortingOrder] = sortorder(array, col)
[sortedArray, sortingOrder] = sortrows(array, col);
That's it, since it's a built-in function. To call, just do (for example):
array = randi(9, 3, 7)
[value, loc] = sortorder(array, 3)
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!