Starting from:

$30

CS1332-Homework 8 Sorting Solved

For this assignment you will be coding 5 different sorts: insertion sort, selection sort, merge sort, quick sort, and LSD radix sort. In addition to the requirements for each sort, to test for efficiency, we will be looking at the number of comparisons made between elements while grading.

For each of the sorting algorithms, you may assume that the arrays you are sorting will not contain null. You should also assume that arrays may contains any number of duplicate elements.

Your implementations must match what was taught in lecture and recitation to receive credit. Implementing a different sort or a different implementation for a sort will receive no credit even if it passes comparison checks.

Comparator
Each method (except radix sort) will take in a comparator and use it to compare the elements of the array in various algorithms described below and in the sorting file. You must use this comparator as the number of comparisons performed with it will be used when testing your assignment.

Generic Methods
Most of the assignments for this class so far have utilized generics by incorporating them into the class declaration. However, the rest of the assignments will have you implement various algorithms as static methods in a utility class. Thus, the generics from here on will use generic methods instead of generic classes (hence the <T in each of the method headers and javadocs).

Inplace Sorts
Some of the sorts below are inplace sorts. This means that the items in the array passed in aren’t copied over to another data structure. Note that you can still create variables that hold only one item; you cannot create another data structure such as array or list in the method.

Stable Sorts
Some of the sorts below are stable sorts. This means that duplicates should remain in the same relative positions after sorting as they were before sorting.

Insertion Sort
Insertion sort should be inplace and stable. It should have a worst case running time of O(n2) and a best case running time of O(n).

Note that, for this implementation, you should sort from the beginning of the array. This means that after the first pass, indices 0 and 1 should be relatively sorted. After the second pass, indices 0-2 should be relatively sorted. After the third pass, indices 0-3 should be relatively sorted, and so on.

Selection Sort
Selection sort should be inplace and unstable. It should have a worst case running time of O(n2) and a best case running time of O(n2). You can implement either the minimum version or the maximum version; both are acceptable since they will both yield the same number of comparisons.

Merge Sort
Merge sort should be out of place and stable. It should have a worst case running time of O(nlogn) and a best case running time of O(nlogn).

LSD Radix Sort
LSD Radix sort should be out of place and stable. It should have a worst case running time of O(kn) and a best case running time of O(kn), where k is the number of digits in the longest number. You will be implementing the least significant digit version of the sort. You will be sorting ints. Note that you CANNOT change the ints into Strings at any point in the sort for this exercise. The sort must be done in base 10. Also, as per the forbidden statements section, you cannot use anything from the Math class besides Math.abs(). However, be wary of handling overflow if you use Math.abs()!

Quick Sort
Quick sort should be inplace and unstable. It should have a worst case running time of O(n2) and a best case running time of O(nlogn). Your implementation must be randomized as specified in the method’s interface.

More products