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

  Creating the gui for the game interface

A GUI-based application that allows a user to play a simple trivia game

  The frantic pipe layer game

Design the Frantic Pipe Layer game

  Study the code and implement the operator overloading

The Table Q3 on the next page is the code of a class named Circle. Study the code and implement the operator overloading for these relational operators ( , >=) for the Circle class. Then, write a test program that creates two instances of the Circle ..

  Java program that reads an input

Create a java program that reads an input of n lines with the first line being the number of people in the contest and the remaining lines a numeric 9 digit code for each person that gets a vote

  Create an array of date objects

Create an array of Date objects of size 4. Initialize the array by using a loop. In the loop, use the Scanner.nextLine() method to input a date as a string, convert it to a date by using the toDate() method, and assign the result to an element in ..

  Write a method called makerow

Write a method called makeRow that is passed two arguments: an int and a String. It returns a String containing n copies of s, concatenated in a row. For instance, if we call the method with makeRow(5, "*"), the method returns *****.

  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..

  Program produces 1000 integer ranging

Then your program produces 1000 integer ranging from 1to 10000 in the array and then sort them in ascending order and then print the result into data.txt file. (You may list integers separated by space or new line)

  Development of a simple program involving multiple classes

Development of a Simple Program Involving Multiple Classes and development of a basic Class, development of the Country and World classes

  Write a java program that accepts a positive long integer

Write a Java program that accepts a positive long integer between 2 to 15, and then using the number, calculate its factorial (n!). Using do-while statement, make sure it only accepts a valid number (between 2 and 15) - if an invalid number is ent..

  Array named peopletypes that can store a maximum

Define a 1-D array named peopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard. Enter a series of 1's, 2's, 3's and 4's into the array, where a 1 represents an infant, a 2 represents a child, a 3 represents..

  Write java program to print strings given at command line

Write a program Average.java which just prints strings that it is given at command line, one per line. If nothing is given at command line, print "No arguments".

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