Question

In this problem, you will write an implementation of BubbleSort. Your function should take in a...

In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in

ascending order.

For example, if you receive the following input followed by a newline:

8 7 6 5 4 3 2 1

then you should display the following output followed by a newline:

1 2 3 4 5 6 7 8

Starter code for reading the input and writing the output has been written for you. Your job is to implement the bubbleSort() function.

Any code that does not explicitly implement the BubbleSort algorithm will receive a score of zero. Additionally, you must implement an algorithm with worst-case running time in O(n^2), best-case running time in Ω(n), and space usage in Θ(1) to receive full credit for design.

//Starter code

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}

// Sort the list
bubbleSort(numbers);

// Print the sorted list
StringBuilder resultSb = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
resultSb.append(new Integer(numbers[i]).toString());
if (i < numbers.length - 1) {
resultSb.append(" ");
}
}
System.out.println(resultSb.toString());
}
  
public static void bubbleSort(int[] numbers) {
// TODO implement this function
  
  





}
}

Homework Answers

Answer #1
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

// Read in the list of numbers
        int[] numbers;
        String input = sc.nextLine();
        if (input.equals("")) {
            numbers = new int[0];
        } else {
            String[] numberStrings = input.split(" ");
            numbers = new int[numberStrings.length];
            for (int i = 0; i < numberStrings.length; i++) {
                numbers[i] = Integer.parseInt(numberStrings[i]);
            }
        }

// Sort the list
        bubbleSort(numbers);

// Print the sorted list
        StringBuilder resultSb = new StringBuilder();
        for (int i = 0; i < numbers.length; i++) {
            resultSb.append(new Integer(numbers[i]).toString());
            if (i < numbers.length - 1) {
                resultSb.append(" ");
            }
        }
        System.out.println(resultSb.toString());
    }

    public static void bubbleSort(int[] numbers) {
        int temp;
        for (int i = 0; i < numbers.length - 1; i++) {
            for (int j = 0; j < numbers.length - 1; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I am trying to take a string of numbers seperated by a single space and covert...
I am trying to take a string of numbers seperated by a single space and covert them into a string array. I have created the following code but it only works if the numbers are seperated a a comma or something similar to that. Example of what I am trying to achieve: string input = "1 1 1 1 1" turn it into.... int[] x = {1,1,1,1} so that it is printed as... [1, 1, 1, 1]    This is...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
Complete the program below. The program takes in two positive integers, n and m, and should...
Complete the program below. The program takes in two positive integers, n and m, and should print out all the powers of n that are less than or equal to m, on seperate lines. Assume both n and m are positive with n less than or equal to m. For example, if the input values are n = 2 and m = 40 the output should be: 2 4 8 16 32 Starter code: import java.util.Scanner; public class Powers {...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
Revise SimpleLinkedListTest Revise the file SimpleLinkedListTest.java given in the lecture in the following ways: Create and...
Revise SimpleLinkedListTest Revise the file SimpleLinkedListTest.java given in the lecture in the following ways: Create and populate a single linked list int values Use this list to test each method you implement in the following ways o Haveatleast1testcaseforsuccessfulcallingthetestedmethod o Haveatleast1testcaseforunsuccessfulcallingthetestedmethod.Forexamplecallingthemethod public int get(int index) and passing an invalid index. NOTE: since the code here is mainly for testing purpose, you do NOT HAVE to use the user input when populating the list and testing your methods. ---------CODE SImpleLinkedListTest------------ package 1234;...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists in main a list   declared as : List<Integer> aList   =   new   List<Integer>(50); Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list. YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements: // swap public static void swap(List<Integer>...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT