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

2 visualizzazioni (ultimi 30 giorni)
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 Commento
the cyclist
the cyclist il 1 Nov 2014
What's your question? Does this code give an error? Not work as expected? Work as expected but slow?

Accedi per commentare.

Risposta accettata

Roger Stafford
Roger Stafford il 1 Nov 2014
function [array2,p] = sortorder(array,col)
[~,p] = sort(array(:,col));
array2 = array(p,:);
return
  3 Commenti
Image Analyst
Image Analyst il 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 il 2 Nov 2014
Modificato: Roger Stafford il 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.

Accedi per commentare.

Più risposte (3)

the cyclist
the cyclist il 1 Nov 2014
If col is the desire sorting order, like [1 3 4 2], then why not just
sorted_array = array(:,col)
?
  6 Commenti
James Tursa
James Tursa il 1 Nov 2014
Modificato: James Tursa il 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 il 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.

Accedi per commentare.


the cyclist
the cyclist il 1 Nov 2014
Modificato: the cyclist il 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 il 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 Commento
Anthony
Anthony il 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.

Accedi per commentare.

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by