Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create the SortAlgs file and Main file separately and please label them Also attempt the bonus and label it and where it goes too thank
Create the SortAlgs file and Main file separately and please label them
Also attempt the bonus and label it and where it goes too thank you and have a nice day
Please don't use a solution already posted thanks you again
*The instructions in the PDF say to use the given SortAlgs.java. That's a mistake! You are supposed to create the sortAlgs file.
Container Class:
public class Container { private int key; private String msg; public Container(int var1) { this(var1, (String)null); } public Container(int var1, String var2) { this.key = var1; this.msg = var2; } public int getKey() { return this.key; } public String getMsg() { return this.msg; } public String toString() { return "" + this.key; } }
Also, try to attempt the bonus that might pop up
Below is the template needed for it
import java.util.Scanner; public class GetBonus { private static final String SECRET_CODE = "tgtscftattfloewittlactaiassb"; private static final String SECRET_CODE_2 = "\"tgtscftattfloewittlactaiassb\""; private static Scanner scanner; private static long startTime; public GetBonus() { } public static void main(String[] var0) throws InterruptedException { scanner = new Scanner(System.in); int[] var2 = new int[]{1, 7, 5}; Container[] var1 = sentence1(); startTimer(); SortAlgs.cocktailSort(var1); printWords(var1, (int)stopTimer() * var2[0]); var1 = sentence2(); startTimer(); SortAlgs.quickSort(var1); printWords(var1, (int)stopTimer() * var2[1]); var1 = sentence3(); startTimer(); SortAlgs.countingSort(var1); printWords(var1, (int)stopTimer() * var2[2]); System.out.println(); System.out.print("Secret Code: "); String var3 = scanner.nextLine().trim().toLowerCase(); if (!var3.equals("tgtscftattfloewittlactaiassb") && !var3.equals("\"tgtscftattfloewittlactaiassb\"")) { System.out.println("\u001b[31mACCESS DENIED!\u001b[0m"); } else { System.out.print("\u001b[32mACCESS GRANTED!\u001b[0m"); sleep(1000); System.out.println(); System.out.println(); textCrawl("**Write the words 'I FOUND THE BONUS' in your header comment so I know you found this**", 30); System.out.println(); sleep(1000); System.out.println(); textCrawl("For 8 Bonus Points (each):", 30); System.out.println(); textCrawl(" Implement Comb Sort, Merge Sort and/or Radix Sort from the extra notes (i.e. Part 3).", 30); System.out.println(); textCrawl(" Add the resulting sorted array to the output", 30); System.out.println(); textCrawl(" in the same format as the other algorithms", 30); System.out.println(); textCrawl(" Also, add the runtime to the end of the timings", 30); System.out.println(); textCrawl(" in the same format as the other algorithms", 30); System.out.println(); System.out.println(); sleep(1000); textCrawl("Each sorting algorithm you implement gives you 8 bonus points for a max of 24 points!", 30); System.out.println(); System.out.println(); } } private static Container[] sentence1() { return new Container[]{new Container(2, "secret"), new Container(0, "the"), new Container(26, "this"), new Container(-5, "To"), new Container(32, "assignment"), new Container(-3, "get"), new Container(12, "for"), new Container(8, "code")}; } private static Container[] sentence2() { return new Container[]{new Container(-1, "the"), new Container(3000, "lines"), new Container(0, "first"), new Container(150, "these"), new Container(5, "of"), new Container(10, "each"), new Container(-8, "Take"), new Container(26, "word"), new Container(200, "three"), new Container(1, "letter"), new Container(105, "in")}; } private static Container[] sentence3() { return new Container[]{new Container(5, "into"), new Container(2, "concat"), new Container(12, "string"), new Container(0, "And"), new Container(2, "them"), new Container(5, "a"), new Container(2, "all"), new Container(9, "single"), new Container(12, "below")}; } private static void printWords(Container[] var0, int var1) throws InterruptedException { Container[] var2 = var0; int var3 = var0.length; for(int var4 = 0; var4 Your objective is to implement the following three algorithms: - From the list of quadratic time algorithms: Cocktail Sort - From the list of linearithmic time algorithms: Quick Sort - From the list of linear time algorithms: Counting Sort All of these algorithms are explained in detail in the notes and some or all of the pseudocode behind them is given. You will create a public class called SortAlgs inside of the given SortAlgs.java file. Inside this class will be three public methods, cocktailsort, quicksort and countingSort. Each of these public methods take in one, and only one parameter - that being an array of container objects (which is described below). The methods do not return anything as they either modify the list "in-place" or generate a new sorted list then copy it over into the given list parameter. You are free to have as many private helper methods as you wish. The compiled Container class is provided as Container.class. The following class diagram shows the relevant members of the Container class: The Container class field called "key" is what you will be using to sort. In other words, an array of Container objects are sorted by their key field. Use getKey () to retrieve this field and use the constructor to set this field. You must have a public class in its own file called Main. This public class will contain the entry point which must do the following: - First generate an array of 20 Container objects, each with a random key ranging from 0 to 150 (inclusively) You will need to look online to figure out how to generate random numbers in a range with Java - Make three exact copies of that array - Send each sorting method its own copy This is so each sorting algorithm can start with the same array contents - Print out the original array of Container objects (only printing the key of each) - Print out the sorted array of Container objects (only printing the key of each) after applying each sorting method See the Sample Output below for more clarification This is used so I can make sure your sorting algorithms work - Now generate an array of 20,000 Container objects each with keys within the same value range as above (i.e. 0 to 150 ) - There will be duplicate keys, but this should not pose a problem for your algorithms - Make three exact copies of this large array - Now use Java to time each algorithm as it runs through its copy of the array You are going to have to look up online how to use Java to get the exact runtime of a block of code It is VERY IMPORTANT that you do NOT include the allocation, initialization or cloning of the array of Container objects in your timing. The only thing I want you to time is the sorting algorithm itself (and maybe the function call to the sorting algorithm). When the algorithm is done (function returns back to main), record the time and move on to timing the next algorithm. - Show the times each algorithm took (in milliseconds) Obviously, we should see Cocktail sort taking the longest amount of time while counting sort takes the least. Quick sort should be somewhat close to the runtime of counting sort. Sample Output: Your output should look exactly like the following (albeit with different numbers for your list and different numbers for your times). Your timings might be different, but they should still be within reason. For example, I expect Cocktail to be in the triple digits, Quick to be in the single digits, and Counting to be the fastest in the low single digits. If your timings do not fall within this range, this is an indication that something is not working right or you aren't timing right. Please ask me for help if you can't figure it out. The spacing, values for n, text, etc must be the same. S javac Main. java \&\& java Main TESTING with n=20 Counting sorted: 024314041435657575867677692117120129138 TIMING with n=20,000 Cocktail took 802.74ms Quick took 4.78ms Counting took 2.49ms
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started