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

  Writing a program that parses

The programming project involves writing a program that parses, using recursive descent, a GUI definition language defined in an input file and generates the GUI that it defines.

  Prepare address book java application

Prepare an application that reads the contents of your address book file and prepare a user guide that includes a description of the functionality of your overall address book system.

  Write program in java-calculate and display mortgage payment

Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage.

  Java program to find a value at in index

Construct a main class named Array Program that generates an array of 50-integers, all ranging from one to one hundred.

  Compute the temperature in centigrade

Compute the temperature in Centigrade - Display the temperatures in both Centrigrade and Fahrenheit with appropriate labels, using the + operator to concatenate the labels with the variables

  Write a method named is all vowels that returns

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

  Identify the first character of the name and lengthof length

Write a Java application that stores the names of your family and friends in a one-dimensional array of Strings. The program should show all names in upper case and lower case, identify the first character of the name, and the lengths of the names.

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

  Find the solution of all these java question

find the solution of all these java question

  Java-s ability to derive new fonts from existing ones

Find out where on your system these font libraries are located. When you do, please specify the operating system and the location (folder/directory) where you found them. Discuss Java's ability to derive new fonts from existing ones.

  Write java program to accept two words as input

Write a Java program that accepts two words as input and determines if one of them is resulting from changing the order of the others' letters.

  Communication with an smtp server

Develop a graphical user interface based java program that can communicate with a real SMTP email server for sending emails

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