Program for Bubble Sort in C++

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.

Also Read:  Program for Quick Sort in C++

Program for Bubble Sort in C++

 

Output

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.

30 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????

    Reply
    1. Suprith

      Actually the input is wrong ,see this is correct one
      for(i=0;I<n;I++)
      {
      for(j=0;j<n-1-i;j++)
      {
      t=….
      ……..
      …….
      }
      for(k=0;k<n!k++)
      cout<<"sorted…..";
      …….
      }}

      Reply
  2. neha bhatt

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

    Reply
    1. Admin Post author

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

      Reply
  3. Rutamvar

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

    Reply
  4. vikram

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

    Reply
  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
    (i=1:i<=n:++I)

    Reply
  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

    Reply
  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];
    arr[j]=arr[j+1];
    arr[j+1]=temp;
    }
    }

    Reply
    1. Admin Post author

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

      Reply
      1. Chayan

        But why the array size can’t be variable??
        If i take the array size of 50, I cannot sort more than 50 elements….so if i want to have array size of my accordance what should i do??

        Reply
        1. Admin Post author

          In that case you have to dynamically allocate memory at run time.

          Reply
  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!

    Reply
    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.

      Reply
  9. Kaylio

    Hi!

    I’m sorry, but what does

    for(i=0;i>a[i];

    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!

    Reply
  10. Subhajit Saha

    why its not working???

    #include

    using namespace std;

    int main()
    {
    int a[10],size,i,j,temp;
    cout<>size;

    cout<<"Enter the array\n";
    for(i=0;i>a[i];

    for(i=1;i<size;++i)
    {
    for(j=0;ja[i+1])
    {
    temp=a[i];
    a[i]=a[i+1];
    a[i+1]=temp;
    }
    }

    cout<<"The sorted array is"<<endl;
    for(i=0;i<size;i++)
    cout<<a[i]<<"\t";
    return 0;
    }

    Reply
    1. Admin Post author

      Your code is wrong, just check the code that I have shared in this article.

      Reply

Leave a Reply

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