Question

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

1)

import java.util.Random;

class CountingSort

{

void sort(char arr[])

{

int n = arr.length;

char output[] = new char[n];

int count[] = new int[256];

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

count[i] = 0;

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

++count[arr[i]];

for (int i=1; i<=255; ++i)

count[i] += count[i-1];

for (int i = n-1; i>=0; i--)

{

output[count[arr[i]]-1] = arr[i];

--count[arr[i]];

}

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

arr[i] = output[i];

}

}

public class Main {

public static char[] getRandomArray(int n) {

char arr[] = new char[n];

Random rand = new Random();

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

arr[i] = (char) (rand.nextInt(26) + 'a');

}

return arr;

}

public static void main(String args[])

{

CountingSort ob = new CountingSort();

char arr[] = getRandomArray(10);

long start = System.currentTimeMillis();

ob.sort(arr);

long end = System.currentTimeMillis();

System.out.println("Time taken for arr of size 10 is: " + (end - start) + "ms");

arr = getRandomArray(50);

start = System.currentTimeMillis();

ob.sort(arr);

end = System.currentTimeMillis();

System.out.println("Time taken for arr of size 50 is: " + (end - start) + "ms");

arr = getRandomArray(100);

start = System.currentTimeMillis();

ob.sort(arr);

end = System.currentTimeMillis();

System.out.println("Time taken for arr of size 100 is: " + (end - start) + "ms");

}

}

NOTE: As per Chegg policy I am allowed to answer specific number of questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
1. Write a Java program to implement Counting 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
  • 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).

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

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

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

  • Write a program in Java that performs the Counting Sort algorithm. a) Create the countingSort method....

    Write a program in Java that performs the Counting Sort algorithm. a) Create the countingSort method. Use the pseudocode shown in the lecture. b) Test your codes by writing a program that uses the following input array: int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2}; c) Your output should display the following versions of the arrays: I. The original input array A II. The counting array C after the counting (lines 4-5 in pseudocode)...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

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

  • DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them...

    DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...

  • C++ Search & Sort

    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 number of swaps it takes to sort the array. Define...

  • Be sure to include the number of total swaps in each sort. Create a total_Time variable...

    Be sure to include the number of total swaps in each sort. Create a total_Time variable is the total time it takes all 1000 iterations to run. DO NOT INCLUE THE TIME IT TAKES TO GENERATE A UNIQUE RANDOM ARRAY EACH LOOP. In java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort and create and sort 1000 different arrays of this size,  timing the run to get...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

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