Question

2. Write a Java program to implement Bucket Sort and write a driver to test it....


2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use
random number generator to generate your input with n = 10, 50, and 100. Verify that
the running time is O(n).



3. In this assignment you will investigate three different variants of algorithms that we have
discussed in the class to solve the selection problem.



Version 1. “Simultaneous minimum and maximum” selection algorithm.
Version 2. Randomized selection algorithm.
Version 3. “Median of Medians” selection algorithm.



Each version should be:
1. Run once on a small size array (say n = 16) with voluminous output at each stage, as a
correctness check.
2. Run 10 times each on arrays of size 50, 100, 200, and 400 on random input. You should
include a counter in each version to count a) comparisons and b) swaps for each version
and each size, find the mean and maximum of your statistics.
3. Repeat part 2 on “almost sorted” arrays. To produce an almost sorted array of size n:

a. Initialize a[ i ] as i;
b. Do n/50 times: randomly choose indices i, j and swap the ith and jth array elements.
Tabulate your results, and draw whatever conclusions you think are appropriate


can some one please give me the answer to third part ?


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

Hi,

2.

Code:

import java.util.Random;

public class Main

{

static int[] sort(int[] my_arr, int highest)

{

// This sort function has written using buble sort logic

int[] Bucket = new int[highest + 1];

int[] sorted_arr = new int[my_arr.length];

for (int i = 0; i < my_arr.length; i++)

Bucket[my_arr[i]]++;

int outPos = 0;

for (int i = 0; i < Bucket.length; i++)

for (int j = 0; j < Bucket[i]; j++)

sorted_arr[outPos++] = i;

return sorted_arr;

}

static int highest(int[] sequence)

{

int highest = 0;

for (int i = 0; i < sequence.length; i++)

if (sequence[i] > highest)

highest = sequence[i];

return highest;

}

public static void driver_func(int N)

{

//using this driver function to test the sort function

int[] ran_arr = new int[N];

Random ran_gen = new Random();

for (int i = 0; i < N; i++)

ran_arr[i] = Math.abs(ran_gen.nextInt(100));

int highest = highest(ran_arr);

System.out.println("\nOriginal Array: ");

for (int i = 0; i < ran_arr.length; i++)

System.out.print(ran_arr[i] + " ");

System.out.println("\nSorted Array: ");

int[] sorted_arr = sort(ran_arr, highest);

for (int i = 0; i < sorted_arr.length; i++)

System.out.print(sorted_arr[i] + " ");

}

public static void main(String args[]){

// testing for different range inputs

driver_func(10);

driver_func(50);

driver_func(100);

}

}

Sample Output:

Original Array:
46 61 32 12 40 79 70 73 28 54
Sorted Array:
12 28 32 40 46 54 61 70 73 79
Original Array:
17 28 88 93 19 10 54 37 85 65 68 6 64 47 7 85 88 83 86 62 86 22 54 78 27 86 54 8 44 81 58 74 83 15 63 7 71 41 7 2 99 98 60 14 37 92 20 37 46 20
Sorted Array:
2 6 7 7 7 8 10 14 15 17 19 20 20 22 27 28 37 37 37 41 44 46 47 54 54 54 58 60 62 63 64 65 68 71 74 78 81 83 83 85 85 86 86 86 88 88 92 93 98 99
Original Array:
31 21 50 90 61 59 96 38 31 41 74 13 99 60 76 9 94 71 69 27 94 45 37 16 45 31 83 54 50 9 77 40 89 83 94 3 35 18 16 81 44 61 13 31 38 25 36 3 23 83 53 77 35 37 25 80 99 58 75 38 30 8 65 48 54 72 39 67 19 75 87 59 1 35 64 86 19 14 97 26 9 45 63 57 42 69 3 6 27 46 15 73 61 32 15 91 25 67 48 27
Sorted Array:
1 3 3 3 6 8 9 9 9 13 13 14 15 15 16 16 18 19 19 21 23 25 25 25 26 27 27 27 30 31 31 31 31 32 35 35 35 36 37 37 38 38 38 39 40 41 42 44 45 45 45 46 48 48 50 50 53 54 54 57 58 59 59 60 61 61 61 63 64 65 67 67 69 69 71 72 73 74 75 75 76 77 77 80 81 83 83 83 86 87 89 90 91 94 94 94 96 97 99 99 

Screenshot for indentation:

To Verify that the order is O(n):

The bucket sort normal order time complexity is O(n+k) where k is the number of buckets that can be formed using the given array input.

since we are using a random number generator for the input array for sort. all the numbers are random falls for n different buckets. so the time complexity will be

O(n+n)-> O(2n)-> further simplifies to O(n)

Note:Only one question at a time. Chegg policy please post question 3 separately. Sorry for the inconvenience.  

Add a comment
Know the answer?
Add Answer to:
2. Write a Java program to implement Bucket Sort and write a driver to test it....
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
  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    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...

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • I need help with this .java implementation: Implement an algorithm that given two sorted arrays of...

    I need help with this .java implementation: Implement an algorithm that given two sorted arrays of size m and n creates a sorted array of size (n + m) in O(n + m) time. The script must take as an input a command line argument specifying the name of a .txt file which will contain the arrays to be sorted. Each line in the .txt file will contain two arrays, with elements in arrays separated by commas and the arrays...

  • Write a complete C++ program that analyzes the following sorts by keeping track of how long...

    Write a complete C++ program that analyzes the following sorts by keeping track of how long they take to sort arrays of longS of size 5000, 10000, and 100000, as well as the number of swaps and comparisons they take in doing so: Insertion sort Selection sort Quick sort shell sort(using gaps of n/2, n/4, n/8, ..., 1) for 20, the gaps would be 10, 5, 2, and 1 shell sort(using gaps based on 2^k + 1, but starting at...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • 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...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in...

    in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in descending order. For example [5, 4, 2]. Return an array with all the elements of these two arrays sorted, also in descending order. partb: if the two arrays as input are size n each. What is the big-O run time and space complexity of your implementation public int[] merge(int[] arr1, int[] arr2){

  • Write a Java program to implement Counting Sort and write a driver to test it. Note:...

    Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n).

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