
starter code
//Provided imports, feel free to use these if needed
import java.util.Collections;
import java.util.ArrayList;
/**
* TODO: add class header
*/
public class Sorts {
/**
* this helper finds the appropriate number of buckets you should allocate
* based on the range of the values in the input list
* @param list the input list to bucket sort
* @return number of buckets
*/
private static int assignNumBuckets(ArrayList<Integer> list) {
Integer max = Collections.max(list);
Integer min = Collections.min(list);
return (int) Math.sqrt(max - min) + 1;
}
/**
* this helper finds the appropriate bucket index that a data should be
* placed in
* @param data a particular data from the input list if you are using
* loop iteration
* @param numBuckets number of buckets
* @param listMin the smallest element of the input list
* @return the index of the bucket for which the particular data should
* be placed in
*/
private static int assignBucketIndex(Integer data, int numBuckets, Integer listMin) {
return (data - listMin) / numBuckets;
}
/**
* This method performs bucket sort on the input arraylist
*
* @param list The arraylist we want to sort
*/
public static ArrayList<Integer> bucketSort(ArrayList<Integer> list) {
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>();
int numBuckets = assignNumBuckets(list);
for (int i = 0; i < numBuckets; i++) {
ArrayList<Integer> freshBucket = new ArrayList<>();
buckets.add(i, freshBucket);
}
Integer listMin = Collections.min(list);
for (Integer data : list) {
int bucketIndex = assignBucketIndex(data, numBuckets, listMin);
// TODO // Adds data to correct bucket
}
ArrayList<Integer> sortedList = new ArrayList<>();
for (ArrayList<Integer> bucket : buckets) {
if (bucket.size() > 0)
Collections.sort(bucket);
//TODO // Adds all elements in bucket to sortedList
}
return sortedList;
}
/**
* This method performs count sort on the input arraylist
*
* @param list The arraylist we want to sort
*/
public static ArrayList<Integer> countSort(ArrayList<Integer> list) {
ArrayList<Integer> output = new ArrayList<Integer>();
// Find the largest element of the array
int max = list.get(0);
for (int i = 1; i < list.size(); i++) {
if (list.get(i) > max)
max = list.get(i);
}
int[] count = new int[max + 1];
// Initialize count array with all zeros.
for (int i = 0; i < max; ++i) {
count[i] = 0;
}
// Initialize output with all zeros.
for (int i = 0; i < list.size(); ++i) {
output.add(0);
}
// Store the count of each element
for (int i = 0; i < list.size(); i++) {
int value = list.get(i);
// TODO // increment count array at correct index
}
// Store the cumulative count of each array
for (int i = 1; i <= max; i++) {
// TODO
}
// Find the index of each element of the original array in count array, and
// place the elements in output array
for (int i = 0; i < list.size(); i++) {
count[list.get(i)]--;
output.set(count[list.get(i)], list.get(i));
}
return output;
}
////////////////////////
///// EXTRA CREDIT /////
////////////////////////
public static boolean inspectInsertion(int[] arr, int n) {
// TODO
return false;
}
public static boolean inspectSelection(int[] arr, int n) {
// TODO
return false;
}
public static boolean inspectMerge(int[] arr, int n) {
// TODO
return false;
}
}
![The idea behind counting sort is to keep track of the counts of each element in the
array, then use those counts to recreate the array in sorted order. For example, if you
know the input array has 2 tens in it and 3 elements less than 10, then you can
conclude that the sorted array will look like this: [x, x, x, 10, 10, ...]. In other words you
know that a 10 must go at index 4, and a 10 must go at index 3. Counting sort is one
sort that uses this concept to sort all elements in an array.
These are videos that show you how Counting Sort works: video1, video2.
Examine the existing implementation. The comments marked as //TODO indicate areas
where specific lines of code are needed to complete the sorting algorithm successfully.](https://content.bartleby.com/qna-images/question/5483afab-7850-40f6-959f-e698dea1419a/bd036467-92d7-4090-8f17-09728fa04555/cvun5fa_thumbnail.png)
![The steps to counting sort are:
-
Find the max element in the input array.
-
-
-
Create a counting array with size equal to the max element + 1. Now each index
of the array serves as a separate counter.
Iterate through the input array, and for each element, increment the
corresponding index in the counting array by 1. For example, if the input array is
[2, 3, 1, 3, 2] the counting array will be updated as follows:
First element [2, 3, 1, 3, 2]: Increment the value at index 2, resulting in [0, 0, 1, 0]
Second element [2, 3, 1, 3, 2]: Increment the value at index 3, resulting in [0, 0, 1, 1]
Third element [2, 3, 1, 3, 2]: Increment the value at index 1, resulting in [0, 1, 1, 1]
Fourth element [2, 3, 1, 3, 2]: Increment the value at index 3, resulting in [0, 1, 1, 2]
Fifth element [2, 3, 1, 3, 2]: Increment the value at index 2, resulting in [0, 1, 2, 2]
Modify the counting array to be a "running total". Meaning the value at an index
k will be the count of elements in the input array that were less than or equal to
k. Using the example array from above: [2, 3, 1, 3, 2], you would get a counting
array: [0, 1, 3, 5]
Input: [0, 1, 2, 2]
Output: 0, 0+1; 0+1+2; 0+1+2+2 producing [0, 1, 3, 5]
Create an output array the same size as the input array.
Iterate through the input array again, for each element reduce its count in the
counting array by 1, then place the element at that index in the output array, for
example with an input array of [2, 3, 1, 3, 2] the iterations would be as follows:
First element [2, 3, 1, 3, 2]: we check the counting array at index 2: [0, 1, 3, 5], and
decrement it -> [0, 1, 2, 5], so 2 will go at index 2 in the output: [0, 0, 2, 0, 0]
Second element [2, 3, 1, 3, 2]: we check the counting array at index 3: [0, 1, 2, 5], and
decrement it -> [0, 1, 2, 4], so 3 will go at index 4 in the output: [0, 0, 2, 0, 3]
Third element [2, 3, 1, 3, 2]: we check the counting array at index 1: [0, 1, 2, 4], and
decrement it -> [0, 0, 2, 4], so 1 will go at index 0 in the output: [1, 0, 2, 0, 3]
Fourth element [2, 3, 1, 3, 2]: we check the counting array at index 3: [0, 1, 2, 4], and
decrement it -> [0, 0, 2, 3], so 3 will go at index 3 in the output: [1, 0, 2, 3, 3]
Fifth element [2, 3, 1, 3, 2]: we check the counting array at index 2: [0, 0, 2, 4], and
decrement it -> [0, 0, 1, 3], so 2 will go at index 1 in the output: [1, 2, 2, 3, 3]
The output array is now sorted and can be returned as is.
Note:
-
you can assume the input will only include positive numbers](https://content.bartleby.com/qna-images/question/5483afab-7850-40f6-959f-e698dea1419a/bd036467-92d7-4090-8f17-09728fa04555/psb038c_thumbnail.png)

Step by stepSolved in 2 steps with 1 images

- Data Structure & Algorithm: Describe and show by fully java coded example how a hash table works. You should test this with various array sizes and run it several times and record the execution times. At least four array sizes are recommended. 10, 15, 20, 25. It is recommended you write the classes and then demonstrate/test them using another classarrow_forwardBinary Search of Strings1. Write a version of the selection sort algorithm presented in the unit, which is usedto search a list of strings.2. Write a version of the binary search algorithm presented in the unit, which isused to search a list of strings. (Use the selection sort that you designed aboveto sort the list of strings.)3. Create a test program that primes the list with a set of strings, sorts the list, andthen prompts the user to enter a search string. Your program should then searchthe list using your binary search algorithm to determine if the string is in the list.Allow the user to continue to search for strings until they choose to exit theprogramarrow_forwardUse stack concepts Q #2 Ô https://www.loc-cs.org/~chu/DataStructu Essay Questions (20% each)- continue [0] [1] [2] [3] myList "Bashful" "Awful" Jumpy "Happy The above "myList" is an ArrayList in Java program. 2. If I want to remove "Awful" from myList, which of the following actions should I take first? a. Move "Happy" to the previous element first. b. Move "Jumpy" to the previous element first. Describe the reason of your choice. Next Pagearrow_forward
- Data structure and algorithms. DYNAMIC DATA STRUCTURE LINKED LIST. PURPOSE OF WORK : Consolidation of knowledge on the theoretical foundations of presentation of data in the form of links, classification of lists, learning to solve problems using the LIST standard template library in C++. Task : Write a program to process a linked list from the standard template library. Perform an individual task as a separate function. Implementation of automatic filling and display functions with random data in the program. The task is : The list is given. Create a function to calculate the arithmetic mean value of the elements of the list equal to the Fibonacci number.arrow_forward1)Write a Java program which stores three values by using singly linked list.- Node class1. stuID, stuName, stuScore, //data fields2. constructor3. update and accessor methods- Singly linked list class which must has following methods:1. head, tail, size// data fields2. constructor3. update and accessor methodsa. getSize() //Returns the number of elements in the list.b. isEmpty( ) //Returns true if the list is empty, and false otherwise.c. getFirstStuID( ), getStuName( ), getFirstStuScore( )d. addFirst(stuID, stuName, stuScore)e. addLast(stuID, stuName, stuScore)f. removeFirst ( ) //Removes and returns the first element of the list.g. displayList( ) //Displays all elements of the list by traversing the linked list.- Test class – initialize a singly linked list instance. Test all methods of singly linked list class. 2. Write a Java program which stores three values by using doubly linked list.- Node class4. stuID, stuName, stuScore, //data fields5. constructor6. update and…arrow_forwardstruct node{int num;node *next, *before;};start 18 27 36 45 54 63 The above-linked list is made of nodes of the type struct ex. Your task is now to Write a complete function code to a. Find the sum of all the values of the node in the linked list. b. Print the values in the linked list in reverse order. Use a temporary pointer temp for a and b. i dont need a full code just the list partarrow_forward
- 6. the grade is under 20 which is outlier, remove it from the array list. 7. Print array list using System.out.println() 8. Use indexOf to print index of 80. 9. Use get function. 10. What is the difference between get and index of? 11. Print the values of the array list using Iterator class. 12.. Delete all the values of the array list using clear function. 13. Print all the values of the array after you execute clear using System.out.println(). what is the result of using clear function? 14. What is the shortcoming of using array List?arrow_forward#Steps: Create a Main class. Declare an ArrayList in the main() method and store the given values in it. Create an array called output of the size of the ArrayList. Iterate through for-loop to get each element from the array List and load it into the array using the get() method of the list. Print the array to see the loaded elements. import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> nameList = new ArrayList<String>(); nameList.add("Robert"); nameList.add("Samson"); nameList.add("Alex"); nameList.add("William"); System.out.println("Elements of List: " + nameList); // Create an array of the same size as nameList Hint: use size() method // Fetch the elements from the nameList and insert into newly created array // Hint: Use get() method to fetch the elements from…arrow_forward# Initialize sets A and BA = { 'a', 'b', 'c', 'd', 'e', 'f' }B = { 'c', 'd', 'f', 'h', 'j', 'k' } print('A = ', sorted(A))print('B = ',sorted(B))print(' ') AsymdiffB = set()# put your code here to compute the symmetric difference of A and B# first add all the elements that are in A but not B# then add all the elements that are in B but not A print('Symmetric Difference of A and B = ', sorted(AsymdiffB))arrow_forward
- 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





