User-Defined Selection Sort for 2-D arrays of any size (MATLAB)

4 views (last 30 days)
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
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?

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 1 Nov 2014
function [array2,p] = sortorder(array,col)
[~,p] = sort(array(:,col));
array2 = array(p,:);
return
  3 Comments
Image Analyst
Image Analyst on 1 Nov 2014
Well I guess my answer below (to use sortrows) was doubly off limits . However you accepted this answer so now we don't know if you're done or not. Do you still need help with your manual sorting method or not?
Roger Stafford
Roger Stafford on 2 Nov 2014
Edited: Roger Stafford on 2 Nov 2014
There are many objections that can be made for the code you show, Anthony. I'll limit myself to just one part - the inner for-loop. You write:
for j = 1:m
j = j + 1;
temp = array(loc,j);
array(loc,j) = array(i,j);
array(i,j) = temp;
end
At the line "j = j + 1" you seem to be trying to assist the for-loop in incrementing j. However, that is its job and it doesn't need such assistance. Also doing so at this point causes an unfortunate offset in the columns to be swapped. Column 1 is ignored and the non-existant column m+1 is referenced which will make matlab unhappy. Moreover this whole operation can easily be accomplished without the for-loop:
temp = array(loc,:);
array(loc,:) = array(i,:);
array(i,:) = temp;
Also a related objection is that this swapping always occurs between an i-th row and the first row as indexed by 'loc' which never changes in your code. That is not how I understand selection sort to operate. According to "http://en.wikipedia.org/wiki/Selection_sort" there is supposed to be swapping between the the minimum value in an unsorted sublist and its first element as this sublist gradually shrinks from left to right, which would imply that 'loc' should be increasing from left to right.

Sign in to comment.

More Answers (3)

the cyclist
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
James Tursa
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
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.

Sign in to comment.


the cyclist
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
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)
  1 Comment
Anthony
Anthony on 1 Nov 2014
I could still use some help. I am trying to follow the attached flow chart to create a selection sort that will sort a selected column and move all of the columns at the same time so that the values in a single row are kept together.

Sign in to comment.

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!