In Java,
Bucket sort is a sorting algorithm that has O(n) performance on average.
I want you to write a function to perform bucket sort on a list of strings.
I also want you to test your function to see if it really work in linear time on average.
This code will need to be tested.
Finally, I want you to compare the performance of your function with Javasort method for lists.
import java.util.*;
class Main{
public static void bucketSort(ArrayList<String> words)
{
int maxlength=0;
for(int i=0;i<words.size();i++)
{
words.set(i,words.get(i).toUpperCase());
if(maxlength<words.get(i).length())
maxlength = words.get(i).length();
}
for(int j=maxlength-1;j>=0;j--)
{
Vector<Vector<String>> map = new Vector<Vector<String>>();
for(int i=0;i<27;i++)
{
map.add(null);
}
for(int i=0;i<words.size();i++)//Add words of of length j or greater to map(which is bucket here)
{
if(words.get(i).length()>j)
{
int val = (int)words.get(i).charAt(j) -65;
if(map.get(val)!= null)
{
map.get(val).add(words.get(i));
}
else
{
Vector<String> vecot = new Vector<String>();
vecot.add(words.get(i));
map.add(val, vecot);
}
}
else///Add words of of length<j to bucket 0
{
if(map.get(0) != null)
{
map.get(0).add(words.get(i));
}
else
{
Vector<String> vecot = new Vector<String>();
vecot.add(words.get(i));
map.add(0, vecot);
}
}
}
int count =0;
for(int i=0;i<map.size();i++)
{
if(map.get(i)!=null)
{
for(int k=0;k<map.get(i).size();k++)
{
words.set(count,map.get(i).get(k));
count++;
}
}
}
}
System.out.println("Next set :");
for(int i=0;i<words.size();i++)
{
System.out.println(words.get(i));
}
}
public static void main(String args[]){
long startTime = System.nanoTime();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
ArrayList<String> a=new ArrayList<String>();
for(int i=0;i<n;i++){
a.add(sc.next());
}
bucketSort(a);
long stopTime = System.nanoTime();
System.out.println("Time taken: "+(stopTime - startTime));
}
}
The time taken by above function is of the order of n, i.e., O(n). The time taken by Java sort method for lists is O(n*log(n)).
In Java, Bucket sort is a sorting algorithm that has O(n) performance on average. I want...
In Java, Write a function to perform bucket sort on a list of strings.Test the function to see if it really works in linear time on average. Finally, compare the performance of the function with Java sort method for lists.
Using Java programming Using System.nanoTime(), compare the performance of bucketsort with the sorting algorithms insertion sort, quicksort, merge sort. Does it really perform faster in practice?
Using Java programming Using System.nanoTime(), compare the performance of bucketsort with the sorting algorithms insertion sort, quicksort, merge sort. Does it really perform faster in practice?
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
. Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....
I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...
c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and implement it as a function. Using the system clock as a timer, determine when the efficiency of the algorithm breaks down. For example, does it become slow after 1000 elements, 10000 elements, 100000 elements? Write some code to figure this out. Then use the sort() algorithm that is part of the STL <algorithm> library. How does this function compare to the function you implemented?...
Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...