Question: USING MATLAB: In this problem, you will implement a basic sorting algorithm. Sorting is the process of rearranging elements in a list in a certain

USING MATLAB:

In this problem, you will implement a basic sorting algorithm. Sorting is the process of rearranging elements in a list in a certain order. Different sorting algorithms exist. The selection sort is a textbook algorithm which is well-known because of its simplicity (and memory-efficiency).

The selection sort algorithm divides an input list into two parts: (1) the sublist of items already sorted is built up from left to right; (2) the sublist of items remaining to be sorted that occupy the rest of the list.

Lets say you want to sort the integer elements in a list in ascending order. When sorting begins, the sorted sublist is empty and the unsorted sublist contains the entire input list, and the algorithm proceeds by populating the sorted sublist while exhausting the unsorted sublist. At each step, the algorithm searches for the smallest element in the unsorted sublist, and puts it in the rightmost position in the sorted sublist which is unoccupied (the position of the first unoccupied element in the sorted sublist gets incremented by one, which also increments the total length of the sorted sublist by one. On the other hand, the unsorted sublist is shortened by one element.

You will write a function selection_sort that implements the selection sort algorithm. It takes a row vector unsorted of an arbitrary length, and returns another row vector sorted of the same length. All entries in unsorted must be present in sorted, but sorted in ascending order.

Caution: You are not allowed to use sort(), min(), max() functions in Matlab. You will not get any points if you do so.

Hint 1: You will need to use two nested loops to implement the selection sort algorithm. In addition, you will need to use two intermediary variables for bookkeeping. First, one variable should keep track of the most recent minimum value in the unsorted list while looping over its elements. Second, you will need another variable to keep track of the location of the most recent minimum value. Both variables are subject to update whenever you find a smaller value in the unsorted list. This way, you can determine the value as well as the location of the smallest element in the unsorted list.

Hint 2: You can horizontally append multiple vectors, say, A, B and C, using the expression [A, B, C] or just [A B C]. You can also vertically append the vectors using the expression [A;B;C].

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement the selectionsort function in MATLAB follow the steps below to perform the sorting Step ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!