Bubble Sort in Descending Order in C++

1. Introduction

In this blog post, we will learn how to write a C++ program to sort an array of integers in a Descending Order using the Bubble Sort algorithm.

Bubble Sort is a straightforward and intuitive algorithm suitable for sorting small datasets. It repetitively traverses through the list, comparing neighboring items, and transposing them if they're in the wrong sequence. By tweaking the comparison condition, Bubble Sort can easily be adapted to sort data in descending order.

2. Program Overview

Our C++ program comprises:

1. swap(): A handy utility to interchange two elements.

2. bubbleSort(): The core function that sorts the array using Bubble Sort.

3. printArray(): A function that outputs the array constituents.

4. main(): The primary function propelling the program.

3. Code Program

#include<iostream>
using namespace std;

// Utility function to swap two elements
void swap(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
}

// Main function to perform bubble sort in descending order
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            // Swap if the current element is less than the next
            if (arr[j] < arr[j+1]) {
                swap(arr[j], arr[j+1]);
            }
        }
    }
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i=0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

// Driver function
int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Original array: \n";
    printArray(arr, n);

    bubbleSort(arr, n);

    cout << "Sorted array in descending order: \n";
    printArray(arr, n);
    return 0;
}

Output:

Original array:
64 34 25 12 22 11 90 
Sorted array in descending order: 
90 64 34 25 22 12 11 

4. Step By Step Explanation

1. swap(): This function serves as a tool to switch two elements. It's called inside our bubble sort function whenever two adjacent elements are out of order.

2. bubbleSort(): The pivotal function of our program. The outer loop operates for every element, while the inner loop runs with diminishing counts. With every cycle of the outer loop, the largest element 'sinks down' to its accurate location. If the current element (arr[j]) is smaller than the succeeding element (arr[j+1]), a swap is executed.

3. printArray(): This simply displays the array elements, facilitating a before-and-after comparison of the sorting.

4. main(): It sets up an example array, prints it, applies the bubble sort, and finally outputs the sorted array.

While the algorithm is transparent and easy to grasp, it's not particularly efficient for larger datasets. Boasting a worst-case and average-case time complexity of O(n^2), it's advisable to opt for more efficient sorting methods, like QuickSort or MergeSort, when dealing with extensive data.

Related C++ Programs on Sorting Algorithms

Comments