Java program that implements an algorithm known as a bubble

Assignment Help JAVA Programming
Reference no: EM13166178

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.

Exercise 1 Description

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.

Exercise 1 Sample Output

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!

Exercise 2 Description

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

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:

  1. Set the current index to 0
  2. Loop over the entire list from the current index to the end, searching for the minimum value
  3. Swap the mimum value with the value at the current index
  4. Increment the current index by 1 and repeat from step 2 until the current index is the final position of the list

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:

  • Set the current index to 0
  • Find the minimum value between the current index and the end of the list
    • This would be the value at index postion 2 (-3)
  • Swap the value at index position 2 with the current index
    • The list is now [-3, 12, 1]
  • Increment the current index by 1 (current index is now 1)
  • The loop now repeats from step 2 - find the minimum value between the current index (1) and the end of the list
    • This would be the value at index position 2 (1)
  • Swap the value at index position 2 with the current index
    • This list is now [-3, 1, 12]
  • Increment the current index by 1 (current index is now 2)
  • The current index is equal to the final index of the list, so the loop ends and the list is sorted

Exercise 2 Sample Output

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? 

 

 

Reference no: EM13166178

Questions Cloud

What value would be returned from call to its size() method : If a collection stores 5 objects, what value would be returned from a call to its size() method?
What are the subscripts in the empirical formula : a compound is 40% C, 6.7% H, and 53.3% O by mass. Assume that we have a 100 g sample of this compound. what are the subscripts in the empirical formula?
Calculate the vapor pressure of water inside pressure cooker : the temperature inside a pressure cooker is 111°C. Calculate the vapor pressure of water inside the pressure cooker.
Review of the audit process and timeline : Discussion of business objectives, risks and key processes and review of the audit process and timeline
Java program that implements an algorithm known as a bubble : 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.
Calculate the number of milliliters that must be added : in the titration of 25.00ml of 0.100M CH3COOH, calculate the number of milliliters of 0.200 M NaOH that must be added to reach a pH of a)3.85, b) 5.25,
If the value for the listprice column is a negative number : If the value for the ListPrice column is a negative number, the stored procedure should raise an error that indicates that this column doesn't accept negative numbers. Similarly, the procedure should raise an error if the value for the DiscountPer..
State ammonia and hydrogen chloride gases react : Ammonia and hydrogen chloride gases react to form solid ammonium chloride. A 10.0 L reaction flask contains ammonia at 0.450 atm and 22°C, and 164 mL of hydrogen chloride gas at 7.50 atm and 271 K is introduced
How many grams of co are required to react : Iron (III) oxide reacts with carbon monoxide to form elemental iron and carbon dioxide. How many grams of CO are required to react with 2.78 grams of Fe2O3?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Java code using jframe

Write a Java code using JFrame. This code needs to manipulate 4 shapes(2 rectangles and 2 squares) in a 400x400 JFrame. This needs to manipulate the shapes in 3 different ways, using 3 different methods. One for translating a shape, one for proportio..

  Java program that will add the corresponding elements

Develop a Java program that will add the corresponding elements of two 1-dimensional arrays X and Y to produce the 1-dimensional array Z. Arrays X, Y, Z have the same dimension [M].

  Class named stock to model a stock

Write a class named Stock to model a stock. The properties and methods of the class are shown in Figure 6.22. The metho changePercent computes the percentage of the change in the current vs. the previous closing price.

  Wrappershallow and wrapperdeep

Each class is simply a wrapper class to hold a private array variable. int [] a; The default constructor for each class should initialize â??aâ??. Each class should have a toString() and equals(). Each class should have a setArray method that allows ..

  Write a method named is all vowels that returns

Write a method named isAllVowels that returns whether a String consists entirely of vowels

  Create a gui version of the game mini sudoku.

Each row has the numbers 1 to 6,Each column has the numbers 1 to 6,Each group of 2 rows by 3 columns also has the numbers 1 to 6.

  Java application to generate three-digit random number

Write a Java application that generates a 3-digit random number 100 times. Display the output for each of the generated numbers and the sum of all 100 numbers.

  Create a web application for sheridan restaurant

Create a web application for Sheridan Restaurant Reviews that contains the following functionality - Display a table of all restaurants based on the city entered by the user

  Write an array where the user can input 7 temperatures

Write an array where the user can input 7 temperatures; include a 'for' loop. Make sure the temperatures are larger than -100 and smaller than or equal to 120, if not, make sure the user enters a valid number. Make sure the temperature is between the..

  Java :a user-defined starting position (other than 0,0)

modifying the program so that it can start from a user-defined starting position (other than 0,0) and search for a user defined ending point (other than row-1, column-1) .

  Create four classes

Create four classes: Account.java, Checking.java, Savings.java, and AccountArray.java.

  Design a program that extends the definition of the class

Design a program that extends the definition of the class JFrame to display a window on the screen. Name your class PropertyTax1, title your window "Calculation of Property Taxes," set the window's width to 400 pixels and height to 300 pixels, and te..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd