Question

PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input...

PROGRAM DESCRIPTION

Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input by radix, bucket sort (with no insertion sort step) once for each radix starting from the least significant. Make sure that your overall implementation is O(n)

NPUT

The input to your program will an unspecified number of entries. Each entry is a non-negative integer containing nine (zero padded) digits ( this means that the integer may have either leading or trailing zeros), one per line. Read your input from STDIN.

OUTPUT

Send the values, one per line, as they were input (all nine digits including leading zeroes, if any), sorted in ascending order, to STDOUT. The only output of your program is the numeric results. (i.e. don't output your name, assignment number, prompts etc.)

NOTES

You may use vectors for your buckets.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class BucketSort {

public static void main(String[] args) {

Scanner getInput = new Scanner(System.in);

System.out.println("Enter total number of values you want to sort");

int arraySize = getInput.nextInt();

List numbers = new ArrayList<>();

System.out.println("Enter " + arraySize + " values");

for (int i = 0; i < arraySize; i++) {

int num = getInput.nextInt();

numbers.add(num);

}

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

for (Integer value : sort(numbers, numbers.size())) {

System.out.println(value);

}

}

public static List sort(List array, int bucketSize) {

if (array.size() == 0) {

return null;

}

// Determine minimum and maximum

valuesInteger minValue = array.get(0);

Integer maxValue = array.get(0);

for (int i = 1; i < array.size(); i++) {

if (array.get(i) < minValue) {

minValue = array.get(i);

}

else if (array.get(i) > maxValue) {

maxValue = array.get(i);

}

}

// Initialise buckets

int bucketCount = (maxValue - minValue) / bucketSize + 1;

List> buckets = new ArrayList>(bucketCount);

for (int i = 0; i < bucketCount; i++) {

buckets.add(new ArrayList());

}

// Distribute input array values into buckets

for (int i = 0; i < array.size(); i++) {

buckets.get((array.get(i) - minValue) / bucketSize).add(array.get(i));

}

// Sort buckets and place back into input array

int currentIndex = 0;

array.clear();

for (int i = 0; i < buckets.size(); i++) {

Integer[] bucketArray = new Integer[buckets.get(i).size()];

bucketArray = buckets.get(i).toArray(bucketArray);

InsertionSort.sort(bucketArray);

for (int j = 0;

j < bucketArray.length; j++) {

array.add(currentIndex++, bucketArray[j]);

}

}

return array;

}

static class InsertionSort {

public static > void sort(T[] array) {

for (int i = 1; i < array.length; i++) {

T item = array[i];

int indexHole = i;

while (indexHole > 0 && array[indexHole - 1].compareTo(item) > 0) {

array[indexHole] = array[--indexHole];

}

array[indexHole] = item;

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....

    Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java. Your program should receive its input from a file named "input.txt", which contains one integer per line. It should produce a sorted output file named "output.txt". Include a main method which demonstrates that your algorithm works.

  • Need a quick solution to this in Java! OPTION 2: RADIX SORT TESTING Write a version...

    Need a quick solution to this in Java! OPTION 2: RADIX SORT TESTING Write a version of Radix Sort to sort an ArrayList or array of ints / Integers that you will place in a file. The integers should all be four digits long. Run the algorithm on at least 3 different files, of different sizes (i.e., each file should have a different number of integers) and have it print the results. The minimum file size should be 20 integers....

  • Description: An ISBN-10 (International Standard Book Number) consists of 10 digits: didzdzdad5d6d7d8d9d1o. The last digit, dio,...

    Description: An ISBN-10 (International Standard Book Number) consists of 10 digits: didzdzdad5d6d7d8d9d1o. The last digit, dio, is a checksum, which is calculated from the other nine digits using the following formula: (d, x 1 + d2 x 2 +d3 x 3 + da x4 + ds x 5 + de x 6 + d7 x 7+ d3 x 8+dex 9) % 11 If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write...

  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...

  • IN PYTHON 3: In this version of Radix Sort we use Queues very naturally. Let us...

    IN PYTHON 3: In this version of Radix Sort we use Queues very naturally. Let us consider the following set of positive integers: 311, 96, 495, 137, 158, 84, 145, 63 We will sort these numbers with three passes. The number of passes is dependent on the number of digits of the largest number - in this case it is 495. In the first pass we will go through and sort the numbers according to the digits in the units...

  • Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values...

    Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values from stdin, separated by one or more spaces or newlines, until reaching EOF 2. The input is guaranteed to be well-formed. 3. The input contains no more than 80 values. 4. on standard output, render a simple vertical column graph representation of the input values, in order left to right, using hash'#' characters as shown in the examples below. The number of hashes printed...

  • Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0...

    Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0 and an array A[0..(n - 1)] of n integers. Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)]. For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • [15 marks] Suppose that students enrolled in one course are required to take four tests, and...

    [15 marks] Suppose that students enrolled in one course are required to take four tests, and each student’s final grade for this course is the average of his/her grades of these four tests. This question asks you to write a program that can be used to compute the lowest final grade, highest final grade and the average final grade. General Requirements: Use a5q1.c as the name of your C source code file. We will use the following command on bluenose...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT