Write a computer program that prompts the user for two numbers, n for the number
ID: 3685006 • Letter: W
Question
Write a computer program that prompts the user for two numbers, n for the number of items in the array to sort, and num_i for a number of iterations. Then do the following:
Initiate a variable running_time to 0
Create a for loop that iterates num_i times.
In the body of the loop,
Create an array of n random integers
Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find them time
Use bubble sort to sort the array
Get the time and set this to end-time
Subtract end-time from start-time and add the result to running_time
Once the program has run, note
The number of items sorted
The number of iterations
The average running time for each size array
Repeat the process nine times, using 50, 250 and 500 as the size of the array, and 100, 1000 and 10,000 as the number of iterations.
Repeat the above steps but this time, use selection sort instead of bubble sort.
===============================================================
Code for Bubble Sort:
----------------------------------------
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("enter number of elements to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted array :");
for (c = 0; c < n; c++)
System.out.println(array[c]);
// your code goes here
}
}
Code for Selection Sort:
-------------------------------------------
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("enter number of elements to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
int arrLength = array.length;
for(int i=0;i
int min=i;
for (int k=i+1;k
if(array[k]
min = k;
}
}
if (i != min) {
int temp=array[i];
array[i]=array[min];
array[min]=temp;
}
}
System.out.println("Sorted array :");
for (c = 0; c < n; c++)
System.out.println(array[c]);
// your code goes here
}
}
Explanation / Answer
Test the solutions with above inputs to find the average time consumed:
Required solution:
import java.util.Random;
import java.util.Scanner;
class BubbleSort {
public static void main( String[] args ) throws java.lang.Exception
{
Random rdm = new Random();
int n, c, d, swap, iter;
Scanner in = new Scanner(System.in);
System.out.println("enter number of elements to sort");
n = in.nextInt();
System.out.println("enter number of iterations");
iter = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for( c = 0; c < n; c++ )
array[c] = rdm.nextInt();
long total = 0;
for( int l = 0; l < iter; l++ )
{
long startTime = System.currentTimeMillis();
for( c = 0; c < (n - 1); c++ )
{
for( d = 0; d < n - c - 1; d++ )
{
if( array[d] > array[d + 1] ) /* For descending order use < */
{
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
long endTime = System.currentTimeMillis();
total += endTime - startTime;
}
System.out.println("Average Time required to sort in milli secs:" + ((double) total / iter));
in.close();
}
}
SelectionSort
import java.util.Random;
import java.util.Scanner;
class SelectionSort {
public static void main( String[] args ) throws java.lang.Exception
{
Random rdm = new Random();
int n, c, iter;
Scanner in = new Scanner(System.in);
System.out.println("enter number of elements to sort");
n = in.nextInt();
System.out.println("enter number of iterations");
iter = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for( c = 0; c < n; c++ )
array[c] = rdm.nextInt();
int arrLength = array.length;
long total = 0;
for( int l = 0; l < iter; l++ )
{
long startTime = System.currentTimeMillis();
for( int i = 0; i < arrLength - 1; i++ )
{
int index = i;
for( int j = i + 1; j < arrLength; j++ )
if( array[j] < array[index] )
index = j;
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
}
long endTime = System.currentTimeMillis();
total += endTime - startTime;
}
System.out.println("Average Time required to sort in milli secs:" + ((double) total / iter));
in.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.