Question

In: Computer Science

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 average case ii. Fill out the array with selected numbers for the best case

Solutions

Expert Solution

// Optimized implementation of Bubble sort

#include <stdio.h>

struct me

{

    int a, b;

};

typedef struct me Struct;

void swap(int *xp, int *yp)

{

    int temp = *xp;

    *xp = *yp;

    *yp = temp;

}

// A utility function to print an array of size n

void printArray(int array[], int n)

{

    int i;

    if (n > 0)

        printf("|");

    for (i = 0; i < n; i++)

        printf(" %d |", array[i]);

    printf("\n\n");

}

// An optimized version of Bubble Sort

Struct bubbleSort(int array[], int n)

{

    Struct t;

    t.a = 0;

    t.b = 0;

    int i, j, s, c, st = 0, ct = 0;

    int swapped;

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

    {

        s = 0;

        c = 0;

        swapped = 0;

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

        {

            c++;

            if (array[j] > array[j + 1])

            {

                swap(&array[j], &array[j + 1]);

                swapped = 1;

                s++;

            }

        }

        printf("\n\nNumber of comparison & swaps in iteration number %d are respectively: %d & %d", i + 1, c, s);

        printf("\nResult after iteration number %d is: ", i + 1);

        printArray(array, n);

        t.a += s;

        t.b += c;

        // IF no two elements were swapped by inner loop, then break

        if (swapped == 0)

        {

            printf("\nAs there's no swapping so, stop the algorithm, This further means that Array is already sorted.");

            break;

        }

    }

    return t;

}

// Driver program to test above functions

int main()

{

    Struct t;

    int array[] = {12, 8, 4, 13, 7, 17, 15, 2};

    int n = sizeof(array) / sizeof(array[0]);

    printf("\nArray Given to me: ");

    printArray(array, n);

    t = bubbleSort(array, n);

    printf("\n\n\nTotal number of comparison & swaps are respectively: %d & %d", t.b, t.a);

    printf("\nFinally Sorted Array: ");

    printArray(array, n);

    return 0;

}

SAMPLE OUTPUT:

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION


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.
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
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...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
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...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT