Question

In: Computer Science

5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...

5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java

Solutions

Expert Solution

n : array length

public void bubbleSort(int A[], int n)

    {

        // Base case

        if (n == 1) return;   

        // One pass of bubble sort. After this pass, the largest element is moved (or bubbled) to end.

        for (int i=0; i < n-1; i++){

            if (A[i] > A[i+1])

            {

                int temp = A[i];

                A[i] = arr[i+1];

                A[i+1] = temp;

            }

       }

        // Largest element is fixed, recur for remaining array

        bubbleSort(A, n-1);

    }

Time Complexity : O(n*n)


Related Solutions

Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Given the following array [17, 15,21,208,16,122,212,53,119,43] Apply bubble sort algorithm and show the status of the...
Given the following array [17, 15,21,208,16,122,212,53,119,43] Apply bubble sort algorithm and show the status of the array after each pass. Also calculate how many comparisons you will be required to pass
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT