Program for Bubble Sort in C++

By | November 19, 2011
In this article you will get program for bubble sort in C++.


Bubble sort is a sorting technique in which each pair of adjacent elements are compared, if they are in wrong order we swap them. This algorithm is named as bubble sort because, same as like bubbles the smaller or lighter elements comes up (at start) and bigger or heavier elements goes down (at end). Below I have shared a program for bubble sort in C++ which sorts a list of numbers in ascending order.

Program for Bubble Sort in C++



C++ Program to sort an Array by using Bubble sort

If you found anything incorrect or have doubts regarding above bubble sort in C++ program then comment below.

25 thoughts on “Program for Bubble Sort in C++

  1. PreejaAkhil

    The input array does not contain the element 7 and the output array missing 23 which is given in the input.. Y its coming so????

  2. neha bhatt

    why do we use 2 for loops in bubble sort program?
    please tell!

    1. Admin Post author

      Because each element is compared with all other elements. Outer loop indicates each element while inner loop indicates comparisons.

  3. Rutamvar

    Why in the first loop it is (i=0;i<n) and the second time it is (i=1;i<n)?

  4. vikram

    dear sir there are no change after sorting.
    the elements are print in same order in which I have entered
    please help…..

  5. Fahad Khalil

    Outer loop indicates each element while inner loop indicates comparisons.

    Since outer loop starts with i=1 & i<n then won't it make the loop run n-1 times only…
    I think to initialise i=1 is necessary because of j<n-1 factor at the inner loop…

    So shouldn't the outer loop be like

  6. Amid

    Dear sir, can you please rewrite the program printing the array step by step, passes by passes and rounds by rounds. And can you please explain to me whats the difference between the two. I just really need to learn more in programming. If you’re not to busy sir/maam

  7. nish

    is this correct for bubble sort?
    void sort(int arr[],int n)
    for(int i=0;i<n;i++)
    { for(int j=0;jarr[j+1])
    { int temp= arr[j];

    1. Admin Post author

      array size can’t be variable, it should be constant.

  8. Anurag

    Sir, in the loops ain”t we supposed to be using “i++” instead of “++i”? Because, in case of prefix (++i), the value of i gets incremented even before a single iteration!

    1. Admin Post author

      In this case both ++i and i++ will do the same work. Problem occurs when we are using it in some arithmetic expression.

  9. Kaylio


    I’m sorry, but what does


    mean? The first input gives the array size, what does this second do? I know it’s to collect all the numbers for the array, but what’s the a[i] for? Thank you!


Leave a Reply

Your email address will not be published. Required fields are marked *