Question

In java create a program to test and measure an actual running time of Insertion sort...

In java create a program to test and measure an actual running time of Insertion sort and Merge sort on your machine. Each sorting algorithm must be a separate method:

a. Randomly generate 100 integers between 0 and 500.

b. Store those integers in an array and display them on screen.

c. Using each sorting algorithm, sort numbers and display the sorted numbers. The array must be passed as an argument to call a method.

d. For each sorting algorithm, measure an actual running time (millisecond or nanosecond) taken in sorting numbers on your machine and save the time in a temporary variable(s) or an array. The actual running time would be the elapsed time from start to end of sorting.

e. Repeat a – d five times at least.

f. Display five running times measured for both sorting algorithms.

g. Calculate an average of the five running times for both and display them. h. All data display must appear in a tabular format.

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

You may wanna change time from milli seconds to nano seconds in case it is displaying all zeros.
The code is self explanatory. Follow up in comments if there are any doubts.

import java.util.*;

public class Sort{

private static int rand(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}

void insertionSort(int arr[]) {
int n = arr.length;
for (int i=1; i<n; ++i){
int key = arr[i];
int j = i-1;
while (j>=0 && arr[j] > key){
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
  
void mergeSort(int arr[], int l, int r){
if (l < r) {
int m = (l+r)/2;
mergeSort(arr, l, m);
mergeSort(arr , m+1, r);
merge(arr, l, m, r);
}
}
  
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
  
for (int i=0; i<n1; ++i) L[i] = arr[l + i];
for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j];
  
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2){
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

public static void main(String[] args) {
Sort obj = new Sort();
int[] arr = new int[100];
long en, insAvg=0, merAvg=0;

for(int i=0;i<5;i++){
for(int i=0;i<arr.size;i++){
arr[i] = rand(0, 500+1);
System.out.print(arr[i]+" ");
}
int[] copy = Arrays.copyOf(arr, arr.length);
long st = System.currentTimeMillis();
insertionSort(copy);
en = System.currentTimeMillis() - st;
System.out.println((i+1) + ". Time taken by Insertion Sort: " + en);
insAvg += en;

int[] copy = Arrays.copyOf(arr, arr.length);
long st = System.currentTimeMillis();
mergeSort(copy, 0, copy.length-1);
en = System.currentTimeMillis() - st;
System.out.println((i+1) + ". Time taken by Merge Sort: " + en);
insAvg += en;
}
  
System.out.println("Average Time taken by insertion sort: " + insAvg/5.0);
System.out.println("Average Time taken by merge sort: " + merAvg/5.0);

}

}

Add a comment
Know the answer?
Add Answer to:
In java create a program to test and measure an actual running time of Insertion sort...
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
  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

  • To measure running time

    Please I need helpe,...1)  General DescriptionIn this assignment you will use and test two array-based sorting algorithms selection sort and merge sort. You will need “System.nanoTime()” to measure running time.  2)  Specific Guidelines1)  Create a new project that contains the following classes:a)  XSort<T extends Comparable<T>>: includes selection sort and merge sort algorithms as methods.b)  Driver: for testing2)  XSort: two main methods should be implemented: selection sort and merge sort. Add any necessary private methods and states that are needed to...

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

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

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

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

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