a)
Code Block | ||
---|---|---|
| ||
function list = listSort(list) sorted = 0; len = length(list)false; while sorted == 0false sorted = 1true; for i = 2:lenlength(list) if list(i) < list(i-1) temp = list(i); list(i) = list(i-1); list(i-1) = temp; sorted = 0false; end end end end |
b)
Code Block | ||
---|---|---|
| ||
function sortedList = insertionSort(list)
len = length(list);
sortedList = zeros(1,len);
while len > 0
temp = -Inf;
for i = 1:len
if list(i) > temp
temp = list(i);
tempI = i;
end
end
sortedList(len) = list(tempI);
len = len-1;
list(tempI) = [];
end
end |
c)
Code Block | ||
---|---|---|
| ||
function mat = sortLists(mat) [l,~] = size(mat); for i =1: l mat(i,:) = insertionSort(mat(i,:)); end end |
d)
Code Block | ||
---|---|---|
| ||
function mat = sortMatrix(mat)
mat = mat';
[x,y] = size(mat);
list = reshape(mat,[1,x*y]); % Gjør matrisen om til en liste
list = insertionSort(list);
mat = reshape(list,[x,y]); % Gjør listen om til matrise igjen
mat = mat'; % Den transponerte matrisen
end |