Bubble Sort Algorithm

how to implement the bubble sort algorithm in C. This article shows a very clean implementation that is easy to understand

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

Sorting is done by comparing each element of a collection with another and swap the two so as to position them correctly. Various algorithms differ only in the strategy used to pick the elements to compare. The bubble sort algorithm is one of the easiest to understand and implement. That's why we have taken it for this chapter. But this algorithm is not the best, there are better options.

Bubble Sort Algorithm

The bubble sort algorithm takes the simplest path by comparing each of the adjacent elements. For example, it starts by comparing the first and second elements, and swapping them if necessary. Then the second and third elements are compared, and so on, till the last two are also compared and swapped if required. Passing: (definition)The processing of comparing and swapping each successive pairs of elements of an array or a colleciton. After the entire array has been passed, another passing is done, and these passings are repeated, and they stop only after a pass in which no swapping was required to be done.

Bubble Sort Step by Step Walkthrough

Suppose we have to sort the following array in increasing ascending order.

3, 0, 5, 7, 2

First compare 3 and 0 and swap them because 0 is smaller than 3. The array becomes

0, 3, 5, 7, 2

Next, 3 and 5 are compared. No swapping required because the ordering is correct. Similarly, 5 and 7 are compared, with no swapping required in this case also. Lastly, 7 and 2 are swapped and we get this array. The first pass is completed.

0, 3, 5, 2, 7

In the next pass, we again begin by comparing the first two elements - 0 and 3, followed by 3 and 5, and when 5 and 2 are compared, swapping is required, and we get this array

0, 3, 2, 5, 7

After this 5 and 7 are compared, and no swapping is required. This completes the second pass. This whole process continues till the entire array gets sorted.

0, 2, 3, 5, 7

C++ Program for Bubble Sort

This is how I implemented it. Of course there are other[and possibly better] implementations. But this one is easier to understand, and it has been completely explained in the video that you can find at the end of this page.

#include <iostream>
 
using namespace std;
 
void printArray(int arr[], int count)
{
    for(int i = 0; i < count; i++)
    {
        printf("%d ", arr[i]);
    }
 
    printf("\n");
}
 
bool swapIfLarger(int& x, int& y)
{
    if(x > y)
    {
        int temp = x;
 
        x = y;
 
        y = temp;
 
        return true;
    }
 
    return false;
}
 
int main()
{
    int arr[] = {3, 0, 5, 7, 2};
 
    cout << "Before bubble sort: ";
 
    printArray(arr, 5);
 
    while(true)
    {
        bool bSwappingDone = false;
 
        for(int i = 0; i < 4; i++)
        {
            if( swapIfLarger(arr[i], arr[i+1]) )
            {
                bSwappingDone = true;
            }
        }
 
        if(!bSwappingDone)
        {
            break;
        }
    }
 
    cout << "After bubble sort: ";
 
    printArray(arr, 5);
 
    return 0;
}

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video Bubble Sort Algorithm



Creative Commons License
This Blog Post/Article "Bubble Sort Algorithm" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05