Question

In: Computer Science

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 largest element in the unsorted part of the array to its correct position in the sorted part of the array. Once at its correct position an element never moves again. An element may change its position many times before moving to its correct position.

BubbleSort(A)

  for outer := A.length - 1 to 1

    for inner := 0 to outer –1

      if A[inner] > A[inner + 1] then

         temp := A[inner]

         A[inner] := A[inner + 1]

         A[inner + 1 := temp

      end if

    end for

  end for

end BubbleSort

use this code

#include <ctime>

#include <iomanip>

#include <iostream>

#include <random>

#include <string>

using namespace std;


bool fill(int a[], int size,

    uniform_int_distribution<int>& u,

    default_random_engine& e)

{

    if (size < 1)

        return false;

    for (int i = 0; i < size; ++i)

            a[i] = u(e);

    

    return true;

}

void show(int a[], int size)

{

    for (int i = 0; i < size; ++i)

        cout << a[i] << ' ';

}

void bubblesort(int a[], int size)

{

}

int main()

{

    const int size = 8;

    default_random_engine e;

    uniform_int_distribution<int> u(10, 99);

    int a1d[size];

    fill(a1d, size, u, e);

    show(a1d, size); cout << endl;

    bubblesort(a1d, size);

    show(a1d, size);

    cout << endl;

    system("pause");

    return 0;

}

Solutions

Expert Solution

code in c++

#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;

bool fill(int a[], int size,
    uniform_int_distribution<int>& u,
    default_random_engine& e)
{
    if (size < 1)
        return false;
    for (int i = 0; i < size; ++i)
            a[i] = u(e); 
    return true;
}

void show(int a[], int size)
{
    for (int i = 0; i < size; ++i)
        cout << a[i] << ' ';
}
void bubblesort(int a[], int size)
{
        int temp;
        for (int i = size-1; i > 0; i--)
        {
                for (int j = 0; j < i; j++)
                {
                        if(a[j]>a[j+1])
                        {
                                temp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=temp;
            }
                }
        }       
}
int main()
{
    const int size = 8;
    default_random_engine e;
    uniform_int_distribution<int> u(10, 99);
    int a1d[size];
    fill(a1d, size, u, e);
    show(a1d, size); cout << endl;
    bubblesort(a1d, size);
    show(a1d, size);
    cout << endl;
    system("pause");
    return 0;
}

(For any doubt in the solution just leave a comment otherwise please press the like button)


Related Solutions

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.
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of...
Write a MIPS program to implement 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 8 as...
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
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...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
compare the time efficiency of the insertion sort algorithm with the bubble sort algorithm. Give the...
compare the time efficiency of the insertion sort algorithm with the bubble sort algorithm. Give the big theta notation of each of the algorithms as a part of your answer.
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT