Ask Java Expert


Home >> Java

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? 

 

 

Java, Programming

  • Category:- Java
  • Reference No.:- M9456296

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As