Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment we will be implementing one of the most popular sorting algorithms used in libraries (like the C++ STL library, and the UNIX

In this assignment we will be implementing one of the most popular sorting algorithms used in libraries (like the C++ STL library, and the UNIX qsort function) to provide basic sorting abilities, the Quicksort algorithm. Quicksort, when properly implemented, is very attractive because it pro- vides a way to do a fast sort completely in-place (without having to allocate additional memory to do the sort, beyond a single value needed when swap- ping two values in the list being sorted). In the worst case, Quicksort is actually O(n2), no better than bubble sort. But this worse case only occurs when every pivot selected is the wort possible, and does not divide the list at all. This is very unlikely to happen, unless you know how the pivot is selected, and specically design the input list to always choose the worst possible pivot. On average the cost of Quicksort is O(n log n), and it is usually very likely that average case performance will result when lists to be sorted are relatively random. The most direct implementation of Quicksort is as a recursive algorithm. Quicksort is an example of a divide and conquer approach to solving the problem of sorting the list. We are given a list of items, A and indexes left and right that indicate a sub-portion of the list to be sorted. left and right indicate the actual indexes, so if the list is a regular C array of integers, and the array is of size 10 int left; int right; const inst SIZE = 10; int A[size]; Then to sort the whole list we set left = 0 and right = 9 to initially call the Quicksort function: left = 0; right = size-1; quicksort(A, left, right); Conceptually the steps of the Quicksort algorithm are as follows: 1. if list size is 0 or 1 (left <= right) return (lists of this size are sorted by denition). 2. Choose a pivot value and swap the pivot value to the end of the list swap(pivotIndex, right) 3. Partition the list. Partitioning means all values less than the pivot value should end up on the left of the list, and all values greater will be on the right. The rst index k where a value >= to the pivot value is at indicates the new left and right side sub-lists. 4. Swap the pivot value to its correct position k swap(k, right) 5. Recursively call Quicksort on the new left and right sub-lists quicksort(A, left, k-1) quicksort(A, k+1, right) Most of the real work happens in the function/code to partition the list. The partitioning of the list, for Quicksort to be an in-place sort, must work by swapping values in-place in the list of items. All values smaller than the pivot value must end up on the left side, and all values greater on the right. The algorithm to partition the list is traditionally done with these steps: 1. do a linear search from the left of list, stop at rst value on left that is >= pivot 2. do a linear search from the right of list, stop at rst value that is < than the pivot. 3. swap(left, right) assuming you were incrementing left and decre- menting right to point to indexes of values that are on wrong sides 4. if left < right goto 1 Conceptually we search from both ends of the list, and when we nd values that are on wrong sides with respect to the pivot value, we swap them. Eventually the search from both ends will meet somewhere. The location where they meet should be the index of the rst value that is >= to the pivot (going from the left side of the list). This is the index where the pivot value should actually go in the list, because all values before this are smaller than the pivot, and all values at or after are greater than the pivot. Thus at the end of partitioning the list on some pivot value, we are able to swap 1 value each time to its correct nal position in the list. But the values to the left and right will not be sorted, thus we call Quicksort recursively on these sub-lists to get them sorted. In order to help you implement your own version of Quicksort, we have broken the problem down into useful sub-functions. If you implement the sub-functions as specied, in the order given, the nal implementation of the Quicksort function is relatively straight forward, using these smaller func- tions. For this assignment you need to perform the following tasks. 1. Write a function called swapListValues(). This functions takes an array of integers as its rst parameter, and two indexes (the left and right indexes). This function does not return a value explicitly. Recall arrays are passed by reference. As the name implies, the two values in the array at the indicated left and right indexes are to be swapped, and since the array is passed by reference, after returning they will be swapped for the caller of this function. 2. Write a function called findAndSwapPivot(). This function takes the same 3 parameters, an array of integers, and two indexes indicating the left and right sides of a sub-portion of the list. The function should nd the value in the middle of the left and right ends, which will be chosen as the pivot. The function should use the previous swapListValues() function to swap the chosen pivot value to the end of the list of integers. This function returns a value. This is dierent from how the textbook implements the nd pivot function. Our function should return the actual pivot value that was chosen (not the pivotIndex, which we know should be the last index of the sub-list after calling this function). 3. Write a function called partitionList(). This will implement the al- gorithm described preciously. This functions takes the 3 same param- eters for the previous functions, an integer array, and left and right indexes for the sub-portion of the list we are currently partitioning. In addition, this function takes a fourth parameter, the pivot value). This function should make use of the swapListValues() function de- ned previously when swapping values in-place in the list of integers. When this function is called, the pivot has been swapped to the end of the sub-portion of the list, so the right index will be one less than this. This function needs to correctly return the index, described as k above, where the pivot value should actually go. At the end, the location where the left search and right search meet will be this index, the nal location found for the pivot value. 4. Finally write a function called quickSort() using the described algo- rithm above. This function will use all of the 3 previous functions to do its work. If implemented correctly, there is almost nothing to be done in this function besides calling the other 3 functions, and recursively calling itself (except to check for the base case of your recursion).

C++

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions