Initiate a variable running_time to 0
Create a for loop that iterates 1000 times.
In the body of the loop,
Create an array of n random integers (Make sure you make the range of the random numbers substantially bigger than the array, ie. if the array size is 500 have the random number generator pick numbers between 1 and 5000. For the largest array have the random number generator pick numbers between 1 and 50,000).
Get the time and set this to start-time (notice the sort is started after each array is built. You want to time the srt process only). You will have to figure out what the appropriate command is in the programming language you are using to find the time (Important: Do not start the timer until after the array is created).
Use bubble sort to sort the array
Get the time and set this to end-time Subtract start-time from end-time and add the result to total_time
Once the program has run, note
The number of items sorted
The average running time for each array (total_time/1000)
Repeat the process using 500, 2500 and 5000 as the size of the array.
import java.util.*;
public class CalculateSortTime{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.print("The arr of items in the array to sort :");
int noOfItems = sc.nextInt();
printTimeBubbleSort(noOfItems);
printTimeSelctionSort(noOfItems);
}
public static void printTimeBubbleSort(int noOfItems){
Random random = new Random();
long totalTime = 0;
for(int i=0;i<1000;i++){
int [] arr = new int [noOfItems];
for (int d = 0 ; d<arr.length ; d++)
{
int randomNumber = random.nextInt(noOfItems*10)+1;
arr[d] = randomNumber;
}
long startTime = System.currentTimeMillis();
arr = BubbleSort(arr);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
totalTime+=elapsedTime;
}
System.out.println("Using BubbleSort The average running time for
each array with size "+noOfItems+" is "+totalTime/1000.0+"
sec");
}
public static void printTimeSelctionSort(int noOfItems){
Random random = new Random();
long totalTime = 0;
for(int i=0;i<1000;i++){
int [] arr = new int [noOfItems];
for (int d = 0 ; d<arr.length ; d++)
{
int randomNumber = random.nextInt(noOfItems*10)+1;
arr[d] = randomNumber;
}
long startTime = System.currentTimeMillis();
arr = SelectionSort(arr);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
totalTime+=elapsedTime;
}
System.out.println("Using Selection Sort The average running time
for each array with size "+noOfItems+" is "+totalTime/1000.0+"
sec");
}
public static int [] BubbleSort(int[] array)
{
int temp;
for(int i = 0 ; i < array.length-1 ; i++){
for ( int j = 0 ; j < array.length-i-1 ; j++){
//sorting
if ( array[j] > array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
public static int [] SelectionSort(int[] arr)
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
return arr;
}
}
Write a computer program that prompts the user for one number, n for the number of...
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...
PLEASE DO BOTH #5 AND #6. The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java,...
The purpose of the project is to perform a timing experiment. You are required to complete the following activities: Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java, C++, C#, or whatever language you...
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using 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 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....
Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive. The program then generates a random number using the random class: If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number, If the users guess matches the random number tell them they win! If the users guess does not match the random number tell them they are wrong. Use a...
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...
Please use C++ Create a program that generates an array of sizes n= 10, 100, 500, 5000 and 25000 items. Your program should populate those arrays with randomly generated integers with a value between 0 and the 2n where n is the size of the array. Create an implementation for the following sort operations. Count Sort Radix-sort
Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....
Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...