Question: *** To do this assignment you are not to use any of the Collections API, or any pre-implemented sort routines in Java. *** In this
*** To do this assignment you are not to use any of the Collections API, or any pre-implemented sort routines in Java. ***
In this assignment you are required to make use of the sorting routines in chapter 8 of your text book. You must ask the user to select which sorting routine he/she wants to use out of those routines implemented in the book (Insertion Sort, Shell Sort, Merge Sort, or Quick Sort). Based on the user input, you need to call the corresponding routine.
You are required to write one main program that will read in any number of integers and store it in an array of ints (not an ArrayList). What you need to do first is to ask the user what sorting routine he/she would like to use. Then you need to ask the user how many integers to be entered as input, and then ask the user to input these integers.
Your program will store these integers into an array of integers first. Then your program is supposed to remove the duplicates out of this array by first sorting the array (using the user-specified sorting routine), and then removing any duplicates.
Your program must not copy these elements into another array to remove the duplicates. The duplicates should be removed in place which means in the same array. Also, you must sort the array first before you start duplicate removal.
Once your array (the same array you started with) has only unique numbers, your program should print out the array followed by a list of all the numbers that were duplicated. The numbers that were duplicated can be stored into another array.
--------
The following would be a sample scenario of running your program:
Make your choice of a sorting routine first. Then continue as follows:
Enter the number of integers: 10
Enter the 10 integers: 7 9 8 1 9 28 23 28 9 8
The resulting array is: 1 7 8 9 23 28
The numbers 8, 9, 28 were duplicated in the input.
----------
public static void main(String[] args) {
//TO DO:
1. create menu
2. choose sort routine
3. ask how many element in array
4. fill array
5. Sort the array
6. check sorted array for duplicates
7. prints sorted array as well as the number that were duplicated
}
//Methods from the book
public static boolean duplicates (Object [] a)
{
for(int i = 0; i { for(int j = i + 1; j< a.length; j++) { if(a[i].equals(a[j])) { return true; //Duplicate found } } } return false; //not found } public static { for(int p = 1; p { AnyType tmp = a[p]; int j = p; for(; j>0 &&tmp.compareTo(a[j-1])<0;j--) { a[j] = a[j-1]; } a[j] = tmp; } } public static { for(int gap = a.length/2; gap>0; gap = gap ==2?1 : (int) (gap/2.2)) { for(int i = gap; i { AnyType tmp = a[i]; int j = i; for(; j>=gap && tmp.compareTo(a[j-gap]) <0; j -=gap) { a[j] = a[j - gap]; } a [j ]= tmp; } } } public static { AnyType [] tmpArray = (AnyType []) new Comparable[a.length]; mergeSort (a, tmpArray, 0, a.length -1); } private static { if(left < right) { int center = (left + right)/2; mergeSort(a, tmpArray, left, center); mergeSort(a, tmpArray, center+1, right); merge(a, tmpArray, left, center +1, right); } } private static { int leftEnd = rightPos -1; int tmpPos = leftPos; int numElments = rightEnd - leftPos +1; //main loop while(leftPos <= leftEnd && rightPos <= rightEnd) { if(a[leftPos ].compareTo(a[rightPos])<= 0) tmpArray[tmpPos++] = a[leftPos++]; else tmpArray[tmpPos++] = a[rightPos++]; while(leftPos <= leftEnd)//copy rest of first half tmpArray[tmpPos++] = a[leftPos++]; while(rightPos <= rightEnd)//copy rest of first half tmpArray[tmpPos++] = a[leftPos++]; //copy tmpArray back for(int i = 0; i { a[rightEnd] = tmpArray[rightEnd]; } } } public static { quickSort(a, 0, a.length-1); } private static final int CUTOFF = 10; private static { if(low + CUTOFF> high) { insertionQSort(a, low, high); } else { //Sort low, middle, high int middle = (low+high)/2; if(a[middle].compareTo(a[low])<0) swapReferences(a, low, middle); if(a[high].compareTo(a[low])<0) swapReferences(a, low, high); if(a[high].compareTo(a[middle])<0) swapReferences(a, middle, high); //place pivot at position high -1 swapReferences(a, middle, high-1); AnyType pivot = a[high -1]; //begin partitioning int i, j; for(i = low, j = high; ;) { while(a[++i].compareTo(pivot)<0) ; while(pivot.compareTo(a[--j])<0) ; if(i>-j) break; swapReferences(a, i , j); } swapReferences(a, i , high-1); quickSort(a, low, i-1); //sort small elements quickSort(a, i+1, high); //sort large elements } } public static final { AnyType tmp = a[i]; a[i] = a[j]; a[j] = tmp; } private static { for(int p = low+1; p <=high; p++) { AnyType tmp = a[p]; int j; for( j = p; j > low && tmp.compareTo( a[ j - 1 ] ) < 0; j-- ) { a[j] = a[j - 1]; a[j] = tmp; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
