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 ?
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.
1. Write a Java program to implement Counting 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...
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 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 (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 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. 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 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 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...
TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL IT WITH 100 RANDOM INTEGERS, HAVING VALUES 0-99. PRINT THE UNSORTED ARRAY IMPLEMENT EACH SORTING ALGORITHM (DO NOT USE THE BUILT-IN LIBRARIES) FOR EACH ALGORITHM, INCLUDE PSEUDOCODE WITH NUMBERED STEPS. IN YOUR CODE, CLEARLY COMMENT WHICH STEP IS BEING PERFORMED BY THE LINE OR BLOCK OF CODE. USE A TIMER TO CHECK HOW LONG EACH ALGORITHM TAKES TO SORT THE ARRAY. THIS SHOULD BE...
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...