Question: MATLAB : Problem Description Below is the strategy and algorithm for bubble sort (this algorithm will sort a list of numbers in ascending order). Strategy:
MATLAB : Problem Description
Below is the strategy and algorithm for bubble sort (this algorithm will sort a list of numbers in ascending order).
Strategy:
Compare the first item in the list with it's neighbor.
If the first item is larger than it's neighbor, swap their positions.
Do this for the next pair of items, and continue to do this until you've reached the end of the list.
Repeat steps 1- 3 once for every item in the list.
For example, if the list has 10 numbers, you would repeat steps 1 - 3 ten times.
Algorithm:
function bubble_sort (array : numbers, size: size of array) returns sorted: numbers sorted in ascending order
for i from 1 to size
for j from 1 to size - 1
if array(j) > array(j+1)
swap array(j) and array(j+1)
end if statement
end for j
end for i
return array
Implement the bubble sort algorithm for your solution. I have done some of it for you. DO NOT CHANGE MY CODE.
%Do not change the following line function [sorted_array, outer_loop, inner_loop] = do_bubble_sort(unsorted_array, size) %unsorted_array is the array that needs to be sorted using Bubble Sort
%Step 2 of the algorithm (You need to finish this.) for outer_loop
%Step 3 of the algorithm (You need to finish this.) for inner_loop %Step 4 of the algorithm. Since we haven't discussed this yet, I'm giving you this part if unsorted_array(inner_loop) > unsorted_array(inner_loop + 1) %Step 5 of the algorithm (You need to do this.) end %end if statement end %end for inner_loop end %end outer_loop
%Step 9 of the algorithm (I've done this already.) sorted_array = unsorted_array; end %end function
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
