In the lecture and textbook, you have come across the bubblesort function, howev
ID: 3589755 • Letter: I
Question
In the lecture and textbook, you have come across the bubblesort function, however there are many sorting algorithms with various advantages and disadvantages. Go to this Mathworks repository and download the zip file with sorting algorithms: http://www.mathworks.com/matlabcentralifileexchange 45125-sorting-methods/ Firstly, open the bubblesort.m function file. Practice creating an array at the command window, and calling the bubblesort function. Notice the subtle differences between this version and the textbook version -ia swap sub-function is created to swap any two values, and the inner for-loop uses index i starting from Once you are satisfied with the operation of the bubblesort function, change the behaviour of the bubblesort so that you start checking from the right (or bottom) instead of the left (or top) of the array. In order to accomplish this, you can choose to start with either bubblesort implementation - either the textbook version or this downloaded version When you alter the behaviour of the bubblesort function as above, this should change the first-pass behaviour from a "lead-ball sort (largest number sinks to the bottom by the end of the first pass) to a truer "bubble-sort (smallest number floats to the top by the end of the first pass). Display the array after each pass-through of the array. Show the output of this to your tutor. Experiment with some of the other sorting algorithms. Can you explain how selectionsort operates? Also, quicksort is used in the MATLAB sort function because of its quick divide-and-conquer approach (using recursion). What other sorting algorithm does it make use of?Explanation / Answer
Selection Sort
---------------------------------------------------------------------------------------------------------------------------------------------------------
Selection sort algorithm starts by comparing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is.
(if number is, say abc .. If a>b , then swap a & b, number is now bac and so on)
Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is comp
ared. This completes the first step of selection sort.
I have shown the below output (per pass) to help you explain Selection Sort -->
29 10 14 37 13 ............................unsorted array
29 10 14 13 37 ............................Pass 1
13 10 14 29 37 ............................Pass 2
13 10 14 29 37 ............................Pass 3
10 13 14 29 37 ............................Pass 4 (sorted array)
*******************Output*******************
>> selectionSort([30 50 10 20 90])
ans =
10 20 30 50 90
-----------------------------------------------------------------------------------------------------------------------------------------------------
For part B of your question, Matlab uses the Quicksort algorithm to sort internally. It is an O(n log n) operation.
-----------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.