wite a MATLAS pregram shat imelements the 4wiekser algorithrw e sorta 3-dimensio
ID: 3752517 • Letter: W
Question
wite a MATLAS pregram shat imelements the 4wiekser algorithrw e sorta 3-dimensional matrix of any given. dimensions in & steps tha the plane taith the louwest sum of all elements Is atthe front &the flare with the highest ann of-all d n2sides at tha back.. . the 3x3KQ matrix A, which has a planes as shown l46 13 8 1S Compute the sunn at each plane. The sum of Ac,i,1) 1 12 318 13 i5 to highest. This-would-result in. our modi fedhatrx4, baing outputted as a ne matrix 6 23 4 13 14 5 16 18 ia -the- rmance program you will be asked to seleet a pio t that minimi ses the ahanees of worst-case oCnt) behaviour, which accurs erse sorted arrays. There be implemented to enere Cn-lo?" early-sorted or nearly rex areExplanation / Answer
function [sorted_A] = my_sort(A)
dim_A = size(A,3);
sum_A = sum(A, [1,2]);
[sum_A, I] = sort(sum_A,3);
temp_A = ones(size(A));
for i = 1:dim_A
temp_A(:,:,i) = A(:,:,I(i));
end
temp_A = reshape(temp_A,[],dim_A);
for i = 1:dim_A
temp_A(:,i) = quick_Sort(temp_A(:,i));
end
sorted_A = reshape(temp_A,size(A));
for i = 1:dim_A
sorted_A(:,:,i) = sorted_A(:,:,i)';
end
end
function sortedArray = quick_Sort(array)
if numel(array) <= 1
sortedArray = array;
return
end
idx=randperm(length(array),1);
temp = array(idx);
array(idx) = array(end);
array(end) = temp;
pivot = array(end);
array(end) = [];
sortedArray = [quick_Sort( array(array <= pivot) ) pivot quick_Sort( array(array > pivot) )];
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.