Question

Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55. Including Algorithm flowchart of the program.

Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55.

Including Algorithm flowchart of the program.

Homework Answers

Answer #1
public class BubbleSort {


    public static void bubbleSort(int[] array) {

        // outer for loop
        for (int i = 0; i < array.length; i++) {

            // inner for loop
            for (int j = 0; j < array.length - i - 1; j++) {

                if (array[j] > array[j + 1]) {
                    // swap the elements
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

    }

    public static void main(String[] args) {

        int array[] = {10, 20, 15, 0, 6, 7, 2, 1, -5, 55};
        System.out.println("Before Sorting");
        for (int num : array) System.out.print(num + " ");
        System.out.println();

        bubbleSort(array);

        System.out.println("\nAfter Sorting");
        for (int num : array) System.out.print(num + " ");
        System.out.println();

    }
}

====================================================================

FLOW CHART ( Please fit the text in the box)

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
Bubble Sort Algorithm in Java in decreasing order
Bubble Sort Algorithm in Java in decreasing order
Write a version of the bubble sort algorithm in a function called bubbleSort that can be...
Write a version of the bubble sort algorithm in a function called bubbleSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *need answer in C++*
In Java: Create a bubble, insertion, selection sort algorithm that sorts the following: 6 9 8...
In Java: Create a bubble, insertion, selection sort algorithm that sorts the following: 6 9 8 12 3 1 7
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort...
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot = first index) algorithms. Compute the CPU processing time for all the algorithms for varying input sizes as follows: N = 102, 103, 104, 105, and 106 Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 103, 1 - 106, 1 – 109, 1 - 1012 Write down your results as a table (with...
Using Python: 1. A simple sorting algorithm is called a "Bubble Sort" because elements bubble around...
Using Python: 1. A simple sorting algorithm is called a "Bubble Sort" because elements bubble around through the list. It is also called a "Sinking Sort" because the larger values "sink" to the end (bottom) of the list. A bubble sort iterates through a list and swaps adjacent pairs if they are in the wrong order. The sort continues until all elements are correctly ordered. Example: bubbleSort([0, 1, 8, 4, 3, 2, 9]) - as it begins to process will...
The bubble sort algorithm discussed in class is used to sort the following sequence of integers:...
The bubble sort algorithm discussed in class is used to sort the following sequence of integers: 47 29 1 9 5 23 • How many passes must the algorithm perform to guarantee the entire sequence is sorted? • What is the list obtained after the first pass? • What is the list obtained after the third pass? • What is the list obtained after the final pass?
Write a Java Program to insert sort 7, 1, 3, 2, 42, 76, 9, then write...
Write a Java Program to insert sort 7, 1, 3, 2, 42, 76, 9, then write the sorted sublist for each step.
Write a version of the selection sort algorithm that can be used to sort a list...
Write a version of the selection sort algorithm that can be used to sort a list of strings alphabetically. (Selection sort for int lists is discussed in Chapter 8.) Write a program to test the function and prompt the user to enter 10 strings. Output the sorted list to the console. *Need answer in C++*
Develop an Algorithm and java program using Design Recipe: 1) Develop a java program to the...
Develop an Algorithm and java program using Design Recipe: 1) Develop a java program to the content of one file to another. Hint 1: read() Hint 2: write() 2) Develop a java program to List all files in a given directory. Hint: list()
Write a MATLAB program that implements the Insertion Sort algorithm. Test your implementation with this set...
Write a MATLAB program that implements the Insertion Sort algorithm. Test your implementation with this set of inputs: 16.2 32.7 -0.5 4.4 21.8 -17.0 6.9 14.1 1.5.