logo Practice-It logo

guavaSort

Language/Type: Java Guava collections
Author: Marty Stepp (on 2013/03/25)

Write a method named guavaSort that accepts an array of strings as a parameter and arranges the strings in the array into sorted ascending order. Specifically, your sorting algorithm should use a Guava Multiset or Multimap to implement a variation of the bucket sort algorithm that will work on strings. Use the Guava collection to count occurrences of strings, similarly to what is done in bucket sort, and then place those strings back into the array in sorted order. For example, suppose your method is passed the following array:

[Farm, Zoo, Car, Apple, Bee, Golf, Bee, Dog, Golf, Zoo, Zoo, Bee, Bee, Apple]

Your collection should store the following occurrences of the strings:

[Apple x 2, Bee x 4, Car, Dog, Farm, Golf x 2, Zoo x 3]

Which you should use to put the strings back into the array in sorted order:

[Apple, Apple, Bee, Bee, Bee, Bee, Car, Dog, Farm, Golf, Golf, Zoo, Zoo, Zoo]

Your code should run in O(N log N) time and use O(N) memory, where N is the number of elements in the array. You may assume that the array passed, and all of the strings in it, are not null. Do not construct any other auxiliary collections other than the single Multiset or Multimap.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.