1) Write a computer program that prompts the user for a number, creates an array
ID: 3701089 • Letter: 1
Question
1) Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then uses bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. Write the program in Java.
2) Repeat 1 but use selection sort this time.
1 and 2 are primarily intended to make sure that your algorithms work.
Once you are convinced your programs work, do the following
3) 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.
-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 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 six times, using 500, 2500 and 5000 as the size of the array for each of the two algorithms.
4) Repeat 3 using selection sort.
Explanation / Answer
//Question 1 Bubble Sort Algorithm
import java.util.Scanner;
class RandomArraySort
{
//Creating bubble sort algorithm
void bubbleSort(int randArray[],int len)
{
int temp = 0;
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len-i-1; j++)
{
if (randArray[j] > randArray[j+1])
{
//Swapping data
int temporary = randArray[j];
randArray[j] = randArray[j+1];
randArray[j+1] = temporary;
}
}
}
System.out.println("Sorted Array");
for (int i=0; i<len; ++i)
{
System.out.print(randArray[i] + " "); //Printing Sorted Array
}
}
//Generating an array of the size provided by user
//Array contains random values
void generateRandomArray(int len)
{
int randArray[] = new int[len];
int max =100;
int min = 1;
int val;
System.out.println("Unsorted Array");
for (int i=0; i<len; ++i)
{
val = (int)(Math.random()*((max-min)+1))+min; //Generating random values
randArray[i] = val;
System.out.print(randArray[i] + " "); //Printing Normal Array
}
System.out.println();
bubbleSort(randArray, len);
}
// Driver method to test above
public static void main(String args[])
{
System.out.println("Bubble Sort Algorithm");
RandomArraySort ob = new RandomArraySort();
Scanner in = new Scanner(System.in);
System.out.println("Enter the array size");
int size=in.nextInt();
ob.generateRandomArray(size);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Question 2 Selection Sort Algorithm
import java.util.Scanner;
class RandomArraySortIns
{
//Creating selection sort algorithm
void selectionSort(int randArray[], int len)
{
for (int i = 0; i < len-1; i++)
{
//get min elements
int min_idx = i;
for (int j = i+1; j < len; j++)
if (randArray[j] < randArray[min_idx])
min_idx = j;
//swap elements
int temp = randArray[min_idx];
randArray[min_idx] = randArray[i];
randArray[i] = temp;
}
System.out.println("Sorted Array");
for (int i=0; i<len; ++i)
{
System.out.print(randArray[i] + " "); //Printing Sorted Array
}
}
//Generating an array of the size provided by user
//Array contains random values
void generateRandomArray(int len)
{
int randArray[] = new int[len];
int max =100;
int min = 1;
int val;
System.out.println("Unsorted Array");
for (int i=0; i<len; ++i)
{
val = (int)(Math.random()*((max-min)+1))+min; //Generating random values
randArray[i] = val;
System.out.print(randArray[i] + " "); //Printing Normal Array
}
System.out.println();
selectionSort(randArray, len);
}
// Driver method to test above
public static void main(String args[])
{
System.out.println("Selection Sort Algorithm");
RandomArraySortIns ob = new RandomArraySortIns();
Scanner in = new Scanner(System.in);
System.out.println("Enter the array size");
int size=in.nextInt();
ob.generateRandomArray(size);
}
}
///Since you have posted multiple questions I have answered only first 2 questions. If required more explanation of code feel free to comment. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.