Question

you will analyse two algorithms for finding the median of an array of integers. You will...

you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results.

the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is to do the following:

1. Insert some timing statements into the program to get the compute time to run the merge sort based algorithm on arrays of diferent size.
2. Perform an experiment on arrays of diferent sizes. Gather the results, and create a chart that maps size vs time. Since this algorithm is O(n lg n), you may assume the time is approximately (cn lg n). Try to estimate the hidden constant c.
3. Insert some timing statements in the recursive linear time program. Use a low threshold, as provided in the original program. Gather the results as you did for the first program and perform a similar analysis.
4. Put both of your charts on the same graph and figure out if the curves intersect, and if yes, at which size.
5. Perform another experiment, this time using arrays with a fixed large size, but varying the threshold. Create a chart of threshold vs total time. Find out the optimal threshold. Is the optimal threshold the same as the one you found by intersecting the curves?
6. Create a report that describes the results your experiments. An experiment report should provide enough details so someone reading your report could reproduce the experiment and arrive at the same conclusion.

************************************************************


public class Selection {
static int threshold = 5;
static int size = 11;
  
public static void main(String[] args) {
int[] a = new int[size];
for (int i=0;i<size;i++)
a[i]=(int)(Math.random()*1000);
int index = (int)(Math.random()*size);
System.out.println("input array");
for (int i=0;i<size;i++)
System.out.println(a[i]);   
int result = linearSelection(a,0,size-1,index);
System.out.println("result: index "+index+" value "+a[index]);
System.out.println("final array");
for (int i=0;i<size;i++)
System.out.println(a[i]);
}
public static void merge(int[]a, int p, int q, int r){
// merges subarrays a[p..q] and a[q+1..r]
int n1=q-p+1;
int n2=r-q;
int[] left = new int[n1];
int[] right = new int[n2];
for (int i=0; i<n1; i++)
left[i]=a[p+i];
for (int i=0; i<n2; i++)
right[i]=a[q+i+1];
int i=0;
int j=0;
int k=p;
while (i<n1 && j<n2)
if (left[i]<=right[j])
a[k++]=left[i++];
else
a[k++]=right[j++];
while (i<n1)
a[k++]=left[i++];
while (j<n2)
a[k++]=right[j++];
}
public static void insertionSort(int[] a, int p, int r){
// sorts subarray a[p..r]
for (int j=p+1; j<=r; j++){
int k=a[j];
int i=j-1;
while (i>=p && a[i]>k){
a[i+1]=a[i];
i--;
}
a[i+1]=k;
}
}
public static void mergeSort(int[] a, int p, int r){
// sorts subarray a[p..r]
if (p<r){
int q=(p+r)/2;
mergeSort(a,p,q);
mergeSort(a,q+1,r);
merge(a,p,q,r);
}
}
public static int selectBySorting(int[] a, int p, int r, int i){
// sorts subarray a[p..r] and returns i
mergeSort(a,p,r);
return i;
}
public static int partition(int[] a, int p, int r){
// partitions array a[p..r] using pivot in a[r]
// returns ending position of pivot
int x=a[r];
int i=p-1;
int temp; // for exchanging
for (int j=p; j<r; j++)
if (a[j]<=x){
i++;
//exchange a[i] and a[j]
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
i++;
//exchange a[i] and a[r]
temp = a[i];
a[i]=a[r];
a[r]=temp;
return i;
}
public static int linearSelection(int[] a, int p, int q, int index) {
// performs selection in linear time
// sets a[index] to the value it would be if subarray a[p..q] were sorted
// returns index
int n=q-p+1;
if (n < threshold)
return selectBySorting(a,p,q,index);
else {
int ngroups = n/5;
int[] medians = new int[ngroups];
for (int i=0; i<ngroups; i++){
insertionSort(a,p+i*5,p+i*5+4);
medians[i]=a[p+i*5+2];
}
int pivot = linearSelection(medians,0,ngroups-1,ngroups/2);
int value=medians[pivot];
int k=p;
while (value!=a[k])
k++;   
//exchange to put pivot at the end
int temp=a[k];
a[k]=a[q];
a[q]=temp;
k=partition(a,p,q);
if (index==k)
return k;
if (index<k)
return linearSelection(a,p,k-1,index);
else
return linearSelection(a,k+1,q,index);
}
}
}

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

public class Selection {
static int threshold = 5;
static int size = 11;
  
public static void main(String[] args) {
int[] a = new int[size];
for (int i=0;i<size;i++)
a[i]=(int)(Math.random()*1000);
int index = (int)(Math.random()*size);
System.out.println("input array");
for (int i=0;i<size;i++)
System.out.println(a[i]);   
int result = linearSelection(a,0,size-1,index);
System.out.println("result: index "+index+" value "+a[index]);
System.out.println("final array");
for (int i=0;i<size;i++)
System.out.println(a[i]);
}
public static void merge(int[]a, int p, int q, int r){
// merges subarrays a[p..q] and a[q+1..r]
int n1=q-p+1;
int n2=r-q;
int[] left = new int[n1];
int[] right = new int[n2];
for (int i=0; i<n1; i++)
left[i]=a[p+i];
for (int i=0; i<n2; i++)
right[i]=a[q+i+1];
int i=0;
int j=0;
int k=p;
while (i<n1 && j<n2)
if (left[i]<=right[j])
a[k++]=left[i++];
else
a[k++]=right[j++];
while (i<n1)
a[k++]=left[i++];
while (j<n2)
a[k++]=right[j++];
}
public static void insertionSort(int[] a, int p, int r){
// sorts subarray a[p..r]
for (int j=p+1; j<=r; j++){
int k=a[j];
int i=j-1;
while (i>=p && a[i]>k){
a[i+1]=a[i];
i--;
}
a[i+1]=k;
}
}
public static void mergeSort(int[] a, int p, int r){
// sorts subarray a[p..r]
if (p<r){
int q=(p+r)/2;
mergeSort(a,p,q);
mergeSort(a,q+1,r);
merge(a,p,q,r);
}
}
public static int selectBySorting(int[] a, int p, int r, int i){
// sorts subarray a[p..r] and returns i
mergeSort(a,p,r);
return i;
}
public static int partition(int[] a, int p, int r){
// partitions array a[p..r] using pivot in a[r]
// returns ending position of pivot
int x=a[r];
int i=p-1;
int temp; // for exchanging
for (int j=p; j<r; j++)
if (a[j]<=x){
i++;
//exchange a[i] and a[j]
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
i++;
//exchange a[i] and a[r]
temp = a[i];
a[i]=a[r];
a[r]=temp;
return i;
}

What you need to do in it or in both the algorithms just add two for loops one inside other inside your main method the loops will start from 0 run ups to 1000 for the sake of computing the time taken in execution in milliseconds an I hope you'll get what u want. It's very time consuming to write whole program so I just copied your program and tell you what to do.

Add a comment
Know the answer?
Add Answer to:
you will analyse two algorithms for finding the median of an array of integers. You will...
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
  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • Write merge method for mergeSort method, it takes the array of data, starting index of first...

    Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • Not sure if I did the mergesort execution correct as well as the merge execution. Can...

    Not sure if I did the mergesort execution correct as well as the merge execution. Can you try explaining it visually sort of like how I was approaching it visually. After that translate it to c++ code if possible. I saw another post that used int *L = new int[n1+1] in the merge function which is the same as int L[n1 +1]correct? Heres my driver function int main () € cout << "Enter length you'd like the array Leendl; int...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • In C++: 1. What is the difference between a deterministic algorithm and a randomized algorithm? 2....

    In C++: 1. What is the difference between a deterministic algorithm and a randomized algorithm? 2. What is the difference between a Las Vegas algorithm (pattern) and a Monte Carlo algorithm? 3. Solve the following recurrences, giving the answer in terms of Big-Oh O() F (n) = 4F ( 1 ) +no F (n) = 2 F (*) +2 4. Given the string "DECEMBER", execute the mergesort algorithm by hand to sort it. Show your work to make it clear...

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