Question

Bubble Sort Algorithm in Java in decreasing order

Bubble Sort Algorithm in Java in decreasing order

Homework Answers

Answer #1
public class BubbleSortDescending {

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

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

    public static void main(String[] args) {
        int[] arr = {9, 12, 17, 19, 21, 15, 6, 5};
        System.out.print("Original array: ");
        print(arr);
        bubbleSort(arr);
        System.out.print("Sorted array: ");
        print(arr);
    }
}

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
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.
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?
Why is insertion sort considered better than bubble sort and does it have any significant impact...
Why is insertion sort considered better than bubble sort and does it have any significant impact on the total run time for N where n is the number of data points? (java)
23 30 35 43 4 10 22 4 Complete the following: a) Use the bubble sort...
23 30 35 43 4 10 22 4 Complete the following: a) Use the bubble sort to sort the values showing the order of the values in the list after every pass of the sorting algorithm.   b) What is the time complexity of the sort (use the number of comparisons required as a measure of time.)    c) Modify the algorithm so it stops if there are no swaps in a complete pass. How many comparisons would be needed in your...
Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0,...
Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                    
The following code implements this algorithm to sort a list of numbers in ascending order. But...
The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num):     for i in range(?):         for j in range(?):             if ?:                 temp = list_num[j]                 list_num[j] = list_num[i]                 list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list)
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order....
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num): for i in range(?): for j in range(?): if ?: temp = list_num[j] list_num[j] = list_num[i] list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list) Modify the three lines of code in the program below so that the output is [-34, 1, 13, 20, 23, 45]
Describe a variation of the merge-sort algorithm that is given a single array, S, as input,...
Describe a variation of the merge-sort algorithm that is given a single array, S, as input, and uses only an additional array, T, as a workspace. No other memory should be used other than a constant number of variables. Compare the time complexity of the variation to the normal merge-sort algorithm.
Which type of sorting algorithm is best used to sort sequentially? Explain
Which type of sorting algorithm is best used to sort sequentially? Explain
In my modified quick-sort algorithm, the floor(n/2) is chosen to be the pivot point. What’s the...
In my modified quick-sort algorithm, the floor(n/2) is chosen to be the pivot point. What’s the running time of my modified quick-sort algorithm on an array of numbers that’s already sorted?