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

조회 수: 2 (최근 30일)
Anthony
Anthony 2014년 11월 1일
편집: Roger Stafford 2014년 11월 2일
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
the cyclist
the cyclist 2014년 11월 1일
What's your question? Does this code give an error? Not work as expected? Work as expected but slow?

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2014년 11월 1일
function [array2,p] = sortorder(array,col)
[~,p] = sort(array(:,col));
array2 = array(p,:);
return
  댓글 수: 3
Image Analyst
Image Analyst 2014년 11월 1일
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 2014년 11월 2일
편집: Roger Stafford 2014년 11월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

the cyclist
the cyclist 2014년 11월 1일
If col is the desire sorting order, like [1 3 4 2], then why not just
sorted_array = array(:,col)
?
  댓글 수: 6
James Tursa
James Tursa 2014년 11월 1일
편집: James Tursa 2014년 11월 1일
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 2014년 11월 1일
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
the cyclist 2014년 11월 1일
편집: the cyclist 2014년 11월 1일
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 2014년 11월 1일
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
Anthony
Anthony 2014년 11월 1일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by