Write a function sortWordsarray that does the following Take
Solution
The test case given has passed and other test cases passed too.
Program
function sortedArray = sortWords(array)
sortedArray = {};
while ~isempty(array)
[m,pos] = findSmallest(array);
array(pos) = [];
sortedArray = [sortedArray m];
end
end
function [m,pos] = findSmallest(array)
m = array{1,1};
pos = 1;
for i=2:length(array)
if(isLessWord(array{1,i},m) == 1)
pos = i;
m = array{1,i};
end
end
end
function less = isLessWord(wordA,wordB)
worda=lower(wordA);
wordb=lower(wordB);
len1=numel(worda);
len2=numel(wordb);
small=worda;
big=wordb;
smallReturn=wordA;
bigReturn=wordB;
less = true;
if(len1>len2)
small=wordb;
big=worda;
smallReturn=wordB;
bigReturn=wordA;
less = false;
end
for i=1:numel(small)
if(small(i)<big(i))
break;
elseif (big(i)<small(i))
less = ~less;
break;
else
continue;
end
end
end
