Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 3, Problem 22C
Write a method, shuffle(A), that rearranges the elements of array A so that every possible ordering is equally likely. You may rely on the nextInt(n) method of the java.util.Random class, which returns a random number between 0 and n − 1 inclusive.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
run the main() method of class Lab11D.
Pls do the SWAPIN LAB 11D and below is the tester
For example, A B C D E F should turn into D E F A B C.
You should assume that the array list has an even number of elements (not necessarily 6).
One solution is to keep removing the element at index 0 and adding it to the back.
Each step would look like this:
A B C D E F
B C D E F A
C D E F A B
D E F A B C
When you run the code, you will find that there is a lot of movement in the array list. Each call to remove(0) causes n - 1 elements to move, where n is the length of the array. If n is 100, then you move 99 elements 50 times, (almost 5000 move operations). That's an inefficient way of swapping the first and second halves. (Learn more about time complexity at the end of the lab).
Come up with a better way in which you swap the elements directly. Hint: How do you swap the two elements A and D?
A B C D E F
D B C A E F
D E C A B F
D E F A B C
Write pseudocode for this algorithm.
Add…
Implement a method named “randomArray” in the file named “BSTTest.java” that tests a specific sorting algorithm. This method will take one parameter:
size of a randomly generated array (int): e.g., 10
This method will:
Generate and return a random array of the length specified by the parameter
The value range of each element should be between 0 and 1000 (inclusive)
>>> randomArray(5).
>>> [1, 2, 3, 7, 10]
Implement a method named “test” in the file named “BSTTest.java”. This method should take advantage of the implemented randomArray and print out information as such:
Testing of BST starts.
Insert 3
Insert 2
Insert 5
Insert 100
Delete 3.
The traverse gives a BST as 3, 5, 100
Testing of BST ends.
Please note that the goal is not to manually code the printings of each line. Try your best to make this method as automatic as possible. There are no limits on what parameters this method shall accept or what it returns. However, please make sure to use the method to…
5. Write a static method largestColumnFirst() that receives as parameter a 2D array numAr,finds the column with the largest sum, and switches it with the first column. For example:6 7 9 4 8 9 7 6 4 83 2 7 4 1 → 7 2 3 4 19 4 5 8 3 5 4 9 8 3
Chapter 3 Solutions
Data Structures and Algorithms in Java
Ch. 3 - Prob. 1RCh. 3 - Write a Java method that repeatedly selects and...Ch. 3 - Prob. 3RCh. 3 - The TicTacToe class of Code Fragments 3.9 and 3.10...Ch. 3 - Prob. 5RCh. 3 - Prob. 6RCh. 3 - Prob. 7RCh. 3 - Prob. 8RCh. 3 - Prob. 9RCh. 3 - Prob. 10R
Ch. 3 - Prob. 11RCh. 3 - Prob. 12RCh. 3 - Prob. 13RCh. 3 - Prob. 14RCh. 3 - Prob. 15RCh. 3 - Prob. 16RCh. 3 - Prob. 17CCh. 3 - Prob. 18CCh. 3 - Prob. 19CCh. 3 - Give examples of values for a and b in the...Ch. 3 - Suppose you are given an array, A, containing 100...Ch. 3 - Write a method, shuffle(A), that rearranges the...Ch. 3 - Suppose you are designing a multiplayer game that...Ch. 3 - Write a Java method that takes two...Ch. 3 - Prob. 25CCh. 3 - Prob. 26CCh. 3 - Prob. 27CCh. 3 - Prob. 28CCh. 3 - Prob. 29CCh. 3 - Prob. 30CCh. 3 - Prob. 31CCh. 3 - Prob. 32CCh. 3 - Prob. 33CCh. 3 - Prob. 34CCh. 3 - Prob. 35CCh. 3 - Write a Java program for a matrix class that can...Ch. 3 - Write a class that maintains the top ten scores...Ch. 3 - Prob. 38PCh. 3 - Write a program that can perform the Caesar cipher...Ch. 3 - Prob. 40PCh. 3 - Prob. 41PCh. 3 - Prob. 42PCh. 3 - Prob. 43P
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Figure 2-26 shows a grade report that is mailed to students at the end of each semester. Prepare an ERD reflect...
Modern Database Management (12th Edition)
Type in and run the 15 programs presented in this chapter. Compare the output produced by each program with the...
Programming in C
In this chapter we saw how the statement copy name1 to name2 could be simulated in Bare Bones. Show how that st...
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
The integrity of a programs output is only as good as the integrity of the programs _____ a. compiler b. progra...
Starting Out with Python (4th Edition)
Write method headers (not the bodies) for the following methods: a. Return a sales commission, given the sales ...
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
State whether each of the following is true or false. If false, explain why. The default case is required in th...
Java How To Program (Early Objects)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write the method printTail() method that accepts an array of integers and an integer index as parameters and prints all the array elements starting from the index passed. For example if printTail(x,5) is invoked, it should print all the elements of the array x starting at x[5].arrow_forwardWrite a Java method that takes an array containing the set of all integers in the range 1 to 52 and shuffles it into random order. Your method should output each possible order with equal probability.arrow_forwardWrite a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4. new code can only be added between existing code. as seen in image.arrow_forward
- Write a helper method, with the signature eliminateZerosinArray[int[]): int[], that takes a single dimensional int array as an argument, and returns a new array that only contains the original values that are not equal to 0. The returned array may be the same size or smaller depending on whether there are values that are equal to 0 in the original array. You may only use standard intl| arrays to solve this.arrow_forwardImplement solutions for the following methods: • getCourseSize() – returns the number of students registered in the course (not in the waitlist). It should maintain the public size variable that keeps track of the number of students registered. • getRegisteredIDs() – returns an array of int[], namely registered student id’s. The length of the array is the size (number of students) in the course. • getRegisteredStudents() – returns an array of type Student[], namely the registered Students. The length of the array is the current size (number of students) of the course. • getWaitlistedIDs() – returns an array of type int[], namely the ids of students in the waitlist. • getWaitlistedStudents() – returns an array of Students in the waitlist. public class Course { public String code; public int capacity; public SLinkedList<Student>[] studentTable; public int size; public SLinkedList<Student> waitlist; public Course(String code) {…arrow_forwardjava Finish the method called printUniqueNumbers() that will take anint [] and print out all of the unique numbers in the array. A number is unique when it is only found in the array one time. This is a void method with an int [] parameterarrow_forward
- 6. In Java Write a static method transpose() that receives as parameter, a 2D square array numArof integers, and swaps the row and column of each element. For example:6 7 8 0 6 3 1 23 2 4 5 → 7 2 5 01 5 8 2 8 4 8 92 0 9 3 0 5 2 3Hint: Once you find the largest-sum column, you will need to swap its entries with the first column,element-by-element.arrow_forwardPlease use java. In this assignment, you will implement a class calledArrayAndArrayList. This class includes some interesting methods for working with Arrays and ArrayLists. For example, the ArrayAndArrayList class has a “findMax” method which finds and returns the max number in a given array. For a defined array: int[] array = {1, 3, 5, 7, 9}, calling findMax(array) will return 9. There are 4 methods that need to be implemented in the A rrayAndArrayList class: ● howMany(int[] array, int element) - Counts the number of occurrences of the given element in the given array. ● findMax(int[] array) - Finds the max number in the given array. ● maxArray(int[] array) - Keeps track of every occurrence of the max number in the given array. ● swapZero(int[] array) - Puts all of the zeros in the given array, at the end of the given array. Each method has been defined for you, but without the code. See the javadoc for each method for instructions on what the method is supposed to do and how…arrow_forwardWrite a void method selectionSortDescendTrace() that takes an integer array, and sorts the array into descending order. The method should use nested loops and output the array after each iteration of the outer loop, thus outputting the array N-1 times (where N is the size). Complete main() to read in a list of up to 10 positive integers (ending in -1) and then call the selectionSortDescendTrace() method. If the input is: 20 10 30 40 -1 then the output is: 40 10 30 20 40 30 10 20 40 30 20 10arrow_forward
- Write a method that returns a new array by eliminating theduplicate values in the array using the following method header:public static int[] eliminateDuplicates(int[] list)Write a test program that reads in 10 integers, invokes the method, and displaysthe distinct numbers separated by exactly one space. Here is a sample run of theprogram: Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2 ↵EnterThe distinct numbers are: 1 2 3 6 4 5arrow_forwardWrite a java method that take two integer array with the same size. This method supposed to return an array that holds merged elements in reverse order. For example:a[]= {10, 20, 30, 40}b[]= {50, 60, 70, 80} This method should return: {80 40 70 30 60 20 50 10}arrow_forwardPlease use java only and public int method. Show the steps with the pictures also. In this assignment, you will implement a class calledArrayAndArrayList. This class includes some interesting methods for working with Arrays and ArrayLists. For example, the ArrayAndArrayList class has a “findMax” method which finds and returns the max number in a given array. For a defined array: int[] array = {1, 3, 5, 7, 9}, calling findMax(array) will return 9. There are 4 methods that need to be implemented in the A rrayAndArrayList class: ● howMany(int[] array, int element) - Counts the number of occurrences of the given element in the given array. ● findMax(int[] array) - Finds the max number in the given array. ● maxArray(int[] array) - Keeps track of every occurrence of the max number in the given array. ● swapZero(int[] array) - Puts all of the zeros in the given array, at the end of the given array. Each method has been defined for you, but without the code. See the javadoc for each…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License