following file into your new lab folder. Follow the instructions from Clsoed Lab
ID: 3552680 • Letter: F
Question
following file into your new lab folder. Follow the instructions from Clsoed Lab 01 to import this file into your ClosedLab10 folder.
You will be writing a simple Java program that implements an algorithm known as a Bubble Sort. A Bubble Sort is a simple sorting algorithm that takes an unsorted array of elements and sorts them into ascending order. This process will be broken down into two parts. For the first part, you will write the code that takes a list of numbers from the user from the console, stores them in an array, and displays them in the order that the user provided. For the second part, you will write a method to sort the array using the Bubble Sort algorithm and then display the final results in sorted order.
Your code will take a list of integers as input from the console, store them in an array, and then display the results in the order that the user provided them.
This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program.
Please enter the number of digits to be stored: 5
Enter integer 0: -1
Enter integer 1: 10
Enter integer 2: 15
Enter integer 3: -6
Enter integer 4: 3
The contents of your array:
Number of digits in array: 5
Digits in array: -1 10 15 -6 3
If the user provides a negative number of digits to be stored, the program should prompt the user to input a non-negative number of digits to store:
Please enter the number of digits to be stored: -1
ERROR! You must enter a non-negative number of digits!
Please enter the number of digits to be sorted: 3
Enter integer 0: 0
Enter integer 1: -5
Enter integer 2: 16
The contents of your array:
Number of digits in array: 3
Digits in array: 0 -5 16
If the user asks to store 0 digits, the program should exit with a simple goodbye message:
Please enter the number of digits to be stored: 0 No digits to store? Goodbye!
Create a copy of your solution for Exercise 1 and name it Lab10b.java in the same ClosedLab10 folder.
For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header:
private static void selectionSort(int[] digits)
It should take an array of digits as input. The program will sort the array of digits in-place using the Selection Sort algorithm described below. "In-place" sorting means that the method will not create a new array - instead it will make changes directly to the array passed into the method. You may find it useful to break the selection sort algorithm up into multiple methods.
The Selection Sort algorithm is the simplest example of a sorting algorithm. A sorting algorithm takes a list of items and returns them in sorted order. Selection sort performs this action by searching through the list for the smallest value in the list, swapping it with the first item in the list. It then repeats this process (swapping the second elements of the list with the second smallest, the third element with the third smallest, etc.) until the entire list has been sorted.
A very simple version of the algorithm for bubble sort is as follows:
We can see this at work with an example. Suppose we have the list [ 1, 12, -3 ] and we want to sort it in ascending order:
This is a sample transcript of what your program should do. Note that the prompts for the user have changed from "store" to "sort" and your prompts should be changed accordingly:
Please enter the number of digits to be sorted: 3
Enter integer 0: 12
Enter integer 1: 0
Enter integer 2: -3
Array before sorting:
Number of digits in array: 3
Digits in array: 12 0 -3
Array after sorting:
Number of digits in array: 3
Digits in array: -3 0 12
A second run of the same program might produce the following output:
Please enter the number of digits to be sorted: 5
Enter integer 0: -12
Enter integer 1: -15
Enter integer 2: 44
Enter integer 3: 0
Enter integer 4: 5
Array before sorting:
Number of digits in array: 5
Digits in array: -12 -15 44 0 5
Array after sorting:
Number of digits in array: 5
Digits in array: -15 -12 0 5 44
Your code should give the appropriate messages if the user attempts to have a negative number of digits sorted or if the user asks to sort 0 digits:
Please enter the number of digits to be sorted: -12
ERROR! You must enter a non-negative number of digits!
Please enter the number of digits to be sorted: 0
No digits to sort? Goodbye!
Explanation / Answer
* Lab10a.java
*
* A program that asks the user for a list of numbers and stores them in an array.
* Used to practice using arrays and breaking code up into methods, and as a first
* step to implementing Lab10b.java - bubblesorting the final array.
*
* @author ENTER YOUR NAMES HERE
*
*/
package osu.cse1223;
import java.util.Scanner;
public class Lab10a {
int x;
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
x=getNumDigits(s);
int[] myArray = new int[x];
getDigits(myArray,s);
displayDigits(myArray);
}
private static int getNumDigits(Scanner inScanner) {
inScanner=in.nextInt();
if(inScanner<0)
{
System.out.println("enter the size greater than 0");
System.exit(0);
}
else return inScanner;
}
private static void getDigits(int[] digits, Scanner inScanner) {
int i=x;
int k=0;
while(i>0)
{
inScanner=in.nextInt();
digits[k]=inScanner;
k++;
i--;
}
}
private static void displayDigits(int[] digits) {
int l=k;
while(l>0)
{system.out.println(digits[l]);
l--;
}
}
private static void bubbleSort(int[] digits) {
int min = 0;
for(int i = 0;i<digits.length;i++)
{
//Assume first element is min
min = i;//Selection sort algorithm says that find the minimum in the
// array, but first element is not minimum.What's point here?
for(int j = i + 1;j<digits.length;j++)
{
int temp = digits[i];
digits[i] = digits[j];
digits[j] = temp;
System.out.println(digits[i]);//I print the in ascending order
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.