Question

In: Computer Science

Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...

Programming Language: JAVA

In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method.

Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers.

This can be done by looping until there are no more swaps being made, or using a nested for loop, meaning the array of length n is checked n2 times.

Sample Run #1

Please enter the # of numbers to be sorted: 4

Enter 1 for int's or 2 for doubles: 2

Enter number: 4

Enter number: 3

Enter number: 2

Enter number: 1

3.0, 4.0, 2.0, 1.0

3.0, 2.0, 4.0, 1.0

2.0, 3.0, 4.0, 1.0

2.0, 3.0, 1.0, 4.0

2.0, 1.0, 3.0, 4.0

1.0, 2.0, 3.0, 4.0

Solutions

Expert Solution

import java.util.Scanner;

public class TestBubbleSort {
   public static void bubbleSort(int arr[]) {
       int n = arr.length;
       int i, j, temp;
       // outer loop to travel through the all elements
       for (i = 0; i < n - 1; i++) {
           printArray(arr);
           // inner loop to compare the outer loop elements
           for (j = 0; j < n - i - 1; j++) {
               // if element at j< than j+1 than swap both
               if (arr[j] > arr[j + 1]) {
                   // swap logic
                   temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
           }
       }

   }

   public static void bubbleSort(double arr[]) {
       double n = arr.length;
       int i, j;
       double temp;
       // outer loop to travel through the all elements
       for (i = 0; i < n - 1; i++) {
           // inner loop to compare the outer loop elements
           printArray(arr);
           boolean swapped = false;
           for (j = 0; j < n - i - 1; j++) {
               // if element at j< than j+1 than swap both
               if (arr[j] > arr[j + 1]) {
                   // swap logic
                   temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
           }
       }

   }

   private static void printArray(int[] arr) {
       for (int i = 0; i < arr.length; i++)
           System.out.print(arr[i] + " ");
       System.out.println();
   }

   private static void printArray(double[] arr) {
       for (int i = 0; i < arr.length; i++)
           System.out.print(arr[i] + " ");
       System.out.println();
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter number of elements: ");
       int n = sc.nextInt();

       System.out.println("Enter 1 for int's or 2 for doubles: ");
       int ch = sc.nextInt();
       if (ch == 1) {
           int arr[] = new int[n];
           for (int i = 0; i < n; i++) {
               System.out.println("Enter number: ");
               arr[i] = sc.nextInt();
           }
           bubbleSort(arr);
           printArray(arr);
       } else {
           double arr[] = new double[n];
           for (int i = 0; i < n; i++) {
               System.out.println("Enter number: ");
               arr[i] = sc.nextDouble();
           }
           bubbleSort(arr);
           printArray(arr);
       }

   }

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Programming language is Java In this second assignment, you will calculate currency conversions from United States...
Programming language is Java In this second assignment, you will calculate currency conversions from United States Dollars (USD) to Indian Rupees (INR) or to Euros (EUR) or to British Pounds (GB) using methods and formatting the results. Use these ratios: 1 USD to 72.282250 INR 1 USD to 0.913465 EUR 1 USD to 0.833335 GBP Your task is to write a program that • displays a menu to choose conversion from dollars to rupees, euros, or pounds. • displays the...
java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1....
java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1. Describe auto-boxing, including why it is useful. (Google for this one) Write a few lines of code that auto-box an int into an Integer, and un-box an Integer to an int. 2. Declare an ArrayList of Strings. Add 5 names to the collection. "Bob" "Susan" ... Output the Strings onto the console using the enhanced for loop. 3. Sort the list using the method...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How...
Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How can I sort the numbers in descending order with different ways? Give ArrayList<String> tel = ["11223344", "55442211", "99881122", "99002211", "34446666", "12342353"] I come up a solution using Collections.sort(tel), but it requires a compare method and I have no idea its contents and also the position of the method. Would you suggest 2 or 3 ways and write the code to achieve my purpose?
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT