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 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.
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 FOLLOWING:
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.
Create a NEW class and repeat the process for selection sort
BubbleSort.java
import java.util.Scanner;
import java.util.Random;
public class BubbleSort
{
//Implementation of bubbleSort and it'll return the
number of swaps
public static int bubbleSort(int[] arr)
{
int swap=0;
for(int
i=0;i<arr.length-1;i++)
{
for(int
j=0;j<arr.length-1-i;j++)
{
if(arr[j]>arr[j+1])
{
swap++;
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return swap;
}
public static void main(String[] args)
{
//Object creation for scanner
class
Scanner scanner=new
Scanner(System.in);
System.out.print("Enter number n:
");
//User input for
n
int n = scanner.nextInt();
//object creation for random
class
Random rand=new Random();
//range should be higher than
n
int range=n*10;
//Initialize total_time to 0
long total_time=0;
//sort for 1000 diiferent arrays
of size n
for(int i=0;i<1000;i++)
{
int[] arr=new
int[n];
for(int
j=0;j<n;j++)
{
arr[j] = rand.nextInt(range);
}
long
start_time=System.currentTimeMillis();
bubbleSort(arr);
long
end_time=System.currentTimeMillis();
total_time+=(end_time-start_time);
}
//output printing
System.out.println("\nNumber of
items sorted: "+n);
System.out.println("\nThe average
running time for each array: "+total_time/1000.0+" milli
seconds");
}
}
output screenshot:
SelectionSort.java
import java.util.Scanner;
import java.util.Random;
public class SelectionSort
{
//Implementation of selectionSort and it'll return the
number of swaps
public static int selectionSort(int[] arr)
{
int swap=0;
for(int
i=0;i<arr.length-1;i++)
{
int min=i;
for(int
j=i+1;j<arr.length;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
if(min!=i)
{
swap++;
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
return swap;
}
public static void main(String[] args)
{
Scanner scanner=new
Scanner(System.in);
System.out.print("Enter number n:
");
int n = scanner.nextInt();
Random rand=new Random();
int range=n*10;
long total_time=0;
for(int i=0;i<1000;i++)
{
int[] arr=new
int[n];
for(int
j=0;j<n;j++)
{
arr[j] = rand.nextInt(range);
}
long
start_time=System.currentTimeMillis();
selectionSort(arr);
long
end_time=System.currentTimeMillis();
total_time+=(end_time-start_time);
}
System.out.println("\nNumber of
items sorted: "+n);
System.out.println("\nThe average
running time for each array: "+total_time/1000.0+" milli
seconds");
}
}
output screenshot:
Be sure to include the number of total swaps in each sort. Create a total_Time variable...
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 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. In the body of the loop, Create an array of n random integers (Make sure...
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...
Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...
Java 1. Create a 1D array of 4096 Unique random integer numbers of 1 to 5 digits. Of those numbers give me the following information: - Mean , Mode, Median, Range. - Five times, ask the user for a number within the range (from above) and record the time to find the number in the range. (Loop count) 2. Using a any sort algorithm, build sorted 2D array of random numbers (1 TO 5 digits ) with the x direction...
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...
Microsoft Excel VBA (Visual Basic for Applications) Programming Language Objectives: Create an array and redim it to a size equal to an assigned value of a variable, populate the array with a series of random numbers, output the array to a message box and to a worksheet. Instructions: - Review the variables already declared. You won't need others. - See comments in the code that will act as your guide. Add new code directly after each comment. - Assign a...
Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...