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.

42 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
    1. Venkat Vadlamani

      A namespace is a declarative region; it helps is used to organise the code. I suppose you can observe the 8th line of the code; the syntax which is derived from .

      std::cout<<"Enter the size of an array: ";

      This part of the code is the actual way of writing, but using the standard namespace (std) we can eliminate "std::" elsewhere in the code, i.e., the compiler automatically used std:: where ever it is necessary.

      Perhaps, it is a very late answer, you've most probably aced the programming language, but I hope it will be helpful for someone else who is reading this comment.

      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
      1. Dwijesh Gowda

        hmm basically i see the problem instead of giving an output as a[j] its written in the program as a[i]. this is the error! cheers if it helped!

        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
      1. Grace Salibay

        Sir, I just want to clarify if the code will still work even though the third for loop hadn’t had brackets? I don’t have codeblocks right now since the compiler was broken. Thank you!

        Reply
        1. Admin Post author

          Yes it will work, when loop body contains only one statement then brackets are optional.

          Reply
  11. vijay

    can we use while loop instead of for loop. please share the coding using while loop.thanks in advance.

    Reply
  12. muhammad Ibrar

    the program which you are mention is wrong
    cout<<"enter the value of arry"<>a[i];
    then we will use the for loop.

    Reply
  13. blue

    int sortlist(vector list){
    bool flag = true; // added in case of event where no swaps are made, therefore sorted.

    for(int pass=0; pass<list.size() && flag; pass++){
    cout<<"Pass:"<<list[pass]<<endl<<"Pos:";
    flag = false;

    for(int pos=0;pos<list.size();pos++){
    cout<<list[pos]<<" ";
    if(list[pass]<list[pos]){

    int tempvar=list[pass];

    list[pass] = list[pos];
    list[pos] = tempvar;
    flag=true;
    }

    }
    cout<<endl<<endl;
    }

    cout<<"Rearranged List:";
    for(int pos=0;pos<list.size();pos++){
    cout << list[pos]<<" ";
    }
    return 0;
    }

    Reply

Leave a Reply

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