C++ program to implement Bubble Sort

#include<iostream>
using namespace std;
void bubble(int *,int);

 

int main()

{

     int n;

     int arr[20];

     cout<<endl<<"Enter how many elements :";

     cin >> n;

     for(int i=0;i<n;i++)

     cin>>arr[i];

     bubble(arr,n);

     for(i=0;i<n;i++)

     cout<<endl<<arr[i];

     return 0;

};

 

void bubble(int *a,int n)

{

     for(int i=0;i<n;i++)

     {

              for(int j=0;j<n;j++)

              {

                  if(a[j]>a[j+1])

                  {

                    int temp;

                    temp=a[j];

                    a[j]=a[j+1];

                    a[j+1]=temp;

                  }

              }

    }

}

Input

Enter how many elements : 8

8  1  2  3  4  9  5  7 

Output

1  2  3  4  5  7  8  9

Leave a Reply

Your email address will not be published.