
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The bubblesort algorithm code and asks the reader to fill in the entries for the array after each step of the sorting process. The code is written in matlab and the input array is [5 4 3 1 2].
Typology: Exercises
1 / 1
This page cannot be seen from the preview
Don't miss anything!
The following code sorts an array using an algorithm called bubblesort.
function x = bubblesort(x)
disp(’======================= INPUT ARRAY =============================’) fprintf(’(Array x) ’); disp(x) disp(’=================================================================’)
n = length(x); step = 0; for i=1:n- for j=1:n-i if x(j) > x(j+1) temp = x(j+1); x(j+1) = x(j); x(j) = temp; end step = step + 1; fprintf(’(Step %2d) ’,step); disp(x); end fprintf(’(Pass %2d) ’,i); disp(x); end If we run the following script, could you fill in the entries for the array after each step?
x = [5 4 3 1 2]; y = bublesort(x);
(Step 1) (Step 2) (Step 3) (Step 4) (Pass 1) (Step 5) (Step 6) (Step 7) (Pass 2) (Step 8) (Step 9) (Pass 3) (Step 10) (Pass 4)