Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

bubble sort algorithm in c++

// Bubble Sort algorithm -> jump to line 21

...

#include <iostream>      // Necessary for input output functionality
#include <bits/stdc++.h> // To simplify swapping process

...

...

/**
* Sort array of integers with Bubble Sort Algorithm
*
* @param arr   Array, which we should sort using this function
* @param arrSZ The size of the array
* @param order In which order array should be sort
*
* @return Sorted array of integers
*/
void bubbleSortInt(double arr[], int arrSz, string order = "ascending")
{
    for (int i = 0; i < arrSz; ++i)
    {
        for (int j = 0; j < (arrSz - i - 1); ++j)
        {
            // Swapping process
            if ((order == "descending") ? arr[j] < arr[j + 1] : arr[j] > arr[j + 1])
            {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
    return; // Optional because it's a void function
} // end bubbleSortInt

...

int main()
{

...

    return 0; // The program executed successfully.

} // end main
Source by github.com #
 
PREVIOUS NEXT
Tagged: #bubble #sort #algorithm
ADD COMMENT
Topic
Name
4+6 =