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 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.
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...
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.
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT