How do you write a function that will sort a one-dimensional array using looping and conditionals without using the sort or sortrow commands?

14 ビュー (過去 30 日間)
In this problem, you are asked to write a function that will sort a one-dimensional array using looping and conditionals without using the sort or sortrow commands. Create a function called MySort.m, which takes as an input a 1-dimensional vector and sorts the values in a specifed order (ascending or descending). If no order is specifed, the default should be to sort in ascending order.
Function Header: function output = MySort(input, method)
The second input to the MySort, method, should be a string 'ascending' or 'descending'. If the function is called with only one argument, i.e. output = MySort(input), then the data in input should be sorted in ascending order. Hint: Use the variable nargin inside MySort and consider making use of the min and max functions and for or while when sorting.
% Test Cases
>> x = [1 5 3 5 2 10];
>> y1 = MySort(x,'ascending')
y1 =
1 2 3 5 5 10
>> y11 = MySort(x)
y11 =
1 2 3 5 5 10
>> y2 = MySort(x, 'descending')
y2 =
10 5 5 3 2 1
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 10 月 8 日
編集済み: Andrei Bobrov 2012 年 10 月 8 日
Hi Jose! Ajay is your classmate?
Matt J
Matt J 2012 年 10 月 8 日
It looks like your MySort implementation is working correctly, given the test cases you've provided. What is the problem?

サインインしてコメントする。

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 10 月 8 日
編集済み: Sean de Wolski 2012 年 10 月 8 日
This oughtta do it. (And steal the win from Greg :) )
function v2 = MySort(v,direction)
[~,~,idx] = unique(v(:));
v2 = cell2mat(accumarray(idx,v(:),[],@(x){x}));
if nargin==2&&strcmp(direction,'descending')
v2 = flipud(v2);
end
for ii = 1
v2 = reshape(v2,size(v));
end
  1 件のコメント
John D'Errico
John D'Errico 2012 年 10 月 8 日
Well, it does use a loop, and it does have a test, and it does not explicitly call sort or sortrows. I gotta give it to Sean. :)

サインインしてコメントする。

その他の回答 (3 件)

Greg Heath
Greg Heath 2012 年 10 月 8 日
編集済み: Greg Heath 2012 年 10 月 8 日
Find minx = min(x) at index indmin
x(indmax) = [];
y1 = [ y1 minx ];
Hope this helps enough so that you will formally accept my answer.
Greg
  1 件のコメント
John D'Errico
John D'Errico 2012 年 10 月 8 日
While this offers a poor scheme that can work when coupled with the proper logic, it must be made to work when the array has duplicates, which will take some careful logic. It is poor because it is terribly inefficient, doing far more tests than one should do in a sort. Note also that that this scheme has arrays that are constantly changing in size, so it will be an allocation nightmare.

サインインしてコメントする。


Jose Canseco
Jose Canseco 2012 年 10 月 8 日
編集済み: Jose Canseco 2012 年 10 月 8 日
function output = MySort(input,method)
for x = 1:length(input)
if(length(input) <= 1)
output=input;
elseif(length(input)>1)
while (length(input)> 1)
if(method(x)==str('ascending'))
output=(min(input):max(input));
elseif(method(x)==str('descending'))
output=(max(input):min(input));
elseif(method(x)==str(''))
output=(min(input):max(input));
end
end
end
end
end
I can't seem to find what the problem is.
  2 件のコメント
John D'Errico
John D'Errico 2012 年 10 月 8 日
But that won't sort the array. It won't even work properly to do what you think it does.
Sean de Wolski
Sean de Wolski 2012 年 10 月 8 日
Here's a hint: assume it's 'ascending' and sort it this way. At the end, you can reverse it if you have to.

サインインしてコメントする。


Matt J
Matt J 2012 年 10 月 8 日

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by