Question: I need help to write following code... Use the Bucket Sort Algorithm to sort a list of strings. Write the code in a file BSort.java.

I need help to write following code... Use the Bucket Sort Algorithm to sort a list of strings. Write the code in a file BSort.java.

Overview of the Problem: Write a method with the header public static void sort(ArrayList strings) with the following features:

1. Use the bucket sort algorithm to sort the array of String objects in the ArrayList object.

2. Assume the characters in the String objects are lowercase characters.

DO NOT PAD THE STRINGS WITH THE SPACE CHARACTER.

Implementation

Part 1: Write a method with the signature public static int charAt(String str, int position) Assume the String str consists of lower-case characters if position < str.length the method returns the ascii value of str.charAt(position) - 96 if postion str.length the method returns 0

Part 2: This code should be written in the sort method. Declare and initialize array of 27 Queue objects Use a loop to determine the greatest length of a String in the array strings. Call this value N. Use the following algorithm to sort the String objects set index to N - 1 while (index >= 0) for(String s: strings) set charIndex = charAt(s, index) queue String s onto Queue[charIndex] end for /* After exiting from thewhile loop, a copy of every String object should be in one of the Queue objects. */ set j = 0 for (bucketNumber = 0 to 26) while (Queue(bucketNumber) != empty) dequeue an element from Queue(bucketNumber) write this element to the ArrayList of Strings at position j (use the set method). increase j by 1. end while end for /* After exiting from the for loop, the size of each of the Queue objects should be zero. */ index = index - 1 end while / * After exiting from the outer while loop, the contents of the ArrayList of String objects will sorted in ascending order. */

Objects: wicker, information, wickerwork, theatre, abbey, paprika, harmless, decelerate, bill, zymogenesis, wicket, beowulf, stibine, chromosome, harold, dissonance, paperweight, viewable, deceive, porcupine

This the class that I have to write my code:--------------------------------

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class BSort {

public static void sort(ArrayList strings) {

//Write code in this method

}

public static void main(String[] args) {

ArrayList arr = new ArrayList<>();

try{

Scanner in = new Scanner(new File(args[0]));

while(in.hasNextLine()){

arr.add(in.nextLine());

}

System.out.println("Unsorted List");

for(String s: arr){

System.out.println(s);

}

sort(arr);

System.out.println(" Sorted List");

for(String s: arr){

System.out.println(s);

}

}

catch(FileNotFoundException f){

System.out.println("File Not Found");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!