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

In the java programming language. How would you find if THREE (3) numbers in an array...
In the java programming language. How would you find if THREE (3) numbers in an array add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 270? YES (200+50+70) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums...
In the java programming language. How would you find if numbers in an array element add...
In the java programming language. How would you find if numbers in an array element add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 210? YES (200+10) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums exist...
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...
Assignment #1: Sorting with Binary Search Tree (IN C LANGUAGE) Through this programming assignment, the students...
Assignment #1: Sorting with Binary Search Tree (IN C LANGUAGE) Through this programming assignment, the students will learn to do the following: 1. Know how to process command line arguments. 2. Perform basic file I/O. 3. Use structs, pointers, and strings. 4. Use dynamic memory. This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree...
in c++ language This assignment is to give you practice using structs and sorting. In competitive...
in c++ language This assignment is to give you practice using structs and sorting. In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores, adding the remaining scores, and then multiplying...
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.
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
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 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!
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT