Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The objective of the assignment is to sort a random array of numbers (i.e. [ 1,

ID: 3534995 • Letter: T

Question

The objective of the assignment is to sort a random array of numbers (i.e. [ 1, 2, 3, 4, 5, ........ , 199, 200] ) WITHOUT actually using sorting commands. The number is also supposed to be very large (e.g. 1,0000,000), so I believe the purpose of the assignment is to compare who's method work's the best and why. In other words, it must be able to sort any amount of numbers as fast as possible. They note we need to code our own algorithm, but isnt that what we're doing? This is my code(script), given below:


function dataOut = Sort(data)

lenD = size(data,2);

ind = cast(floor(lenD/2),'uint8');


j = 1;

k = 1;

L = [];

R = [];


if(lenD<2)

dataOut = data;


else

pivot = data(ind);

for i=1:lenD

if(i~=ind)

if(data(i)<pivot)

L(j) = data(i);

j = j+1;

else

R(k) = data(i);

k = k+1;

end

end

end

L = Sort(L);

R = Sort(R);


dataOut = [L pivot R];


end



When I let the code run, nothing works--and the warnings i get on the script read, "The variable 'L' appears to change sizew on every loop iteration. Consider preallocating for speed." The same goes for 'R'. Note I am not using the 2013 version--I am using the 2012 version. Also note that the point of this excercise is to SORT the numbers as fast as possible. Please try to explain what I've done wrong or write me a new/renewed code for 5 stars.

Explanation / Answer

Here you go man. This isn't much difficult. Only thing you will have to do is implement a sorting algorithm with using inbuilt sort function. Here is the block of code


//************************************************************************************************888


%This function helps sort numbers in ascending and descending order.

%To put in descending order, user must include 'd' in the input.


x = cell2mat(varargin);

y = nargin;

sorted = 0;

n = 0;

while ~sorted

sorted = 1;

for i = 1:y-1

if x(i) > x(i+1)

n = n + 1;

t = x(i);

x(i) = x(i+1);

x(i+1) = t;

sorted = 0;

end

end

end


% else

%

% while ~sorted

% sorted = 1;

% for i = 1:y-1

% if x(i) < x(i+1)

% n = n + 1

% t = x(i);

% x(i) = x(i+1);

% x(i+1) = t;

% sorted = 0;

% end

% end

% end

% end


if nargout > 2

disp('Too many output arguments.')

return

end


for i = 1:nargout

varargout{1} = x;

varargout{2} = n;

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote