1. Write a computer program that prompts the user for a number, creates an array
ID: 3593523 • 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 the 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
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 50, 250 and 500 as the size of the array for each of the two algorithms.
4. Repeat 3 using selection sort.
5. Create a spreadsheet showing the results of 3 and 4 and create a graph to graphically represent the information. Show both sort algorithms on the same graph for comparison.
6. Write a one page document explaining the results, bearing in mind that both algorithms have a complexity of O(n^2) and what you know about complexity analysis. Use your knowledge of complexity analysis to explain your results.
Explanation / Answer
Question 1:
package chegganswers;
import java.util.Random;
import java.util.Scanner;
public class SortArrayOfRandomNnumbers {
public static void bubbleSort(int[] arrayIn) {
int n = arrayIn.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arrayIn[j - 1] > arrayIn[j]) {
temp = arrayIn[j - 1];
arrayIn[j - 1] = arrayIn[j];
arrayIn[j] = temp;
}
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the Number : ");
int userInput = input.nextInt();
int[] randomArray = new int[userInput];
System.out.println("The Generated Array Before Sort.....");
Random random = new Random();
for (int i = 0; i < userInput; i++) {
randomArray[i] = random.nextInt();
System.out.print(randomArray[i] + " ");
}
System.out.println(" The Array After doing Bubble Sort.....");
bubbleSort(randomArray);
for (int i = 0; i < userInput; i++) {
System.out.print(randomArray[i] + " ");
}
}
}
************************************************************************************************************************
Question 2:
package chegganswers;
import java.util.Random;
import java.util.Scanner;
public class SortArrayOfRandomNnumbers {
public static void selectionSort(int[] arrayIn) {
for (int i = 0; i < arrayIn.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arrayIn.length; j++)
if (arrayIn[j] < arrayIn[index]) {
index = j;
}
int small = arrayIn[index];
arrayIn[index] = arrayIn[i];
arrayIn[i] = small;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the Number : ");
int userInput = input.nextInt();
int[] randomArray = new int[userInput];
System.out.println("The Generated Array Before Sort.....");
Random random = new Random();
for (int i = 0; i < userInput; i++) {
randomArray[i] = random.nextInt();
System.out.print(randomArray[i] + " ");
}
System.out.println(" The Array After doing selection sort.....");
selectionSort(randomArray);
for (int i = 0; i < userInput; i++) {
System.out.print(randomArray[i] + " ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.