Bubble Sort in DSA C++

Program 1

// Implementation of Bubble Sort
#include<iostream>
using namespace std;
int main()
{
    system("cls");
    int ar[500],n,i,j,temp;
    cout<<"Enter the limit: ";
    cin>>n;
    cout<<"\n Enter the elements: ";
    for(i=0;i<n;i++)
    cin>>ar[i];
    for(i=0;i<n;i++)
    {
         for(j=0;j<n-i-1;j++)
         {
             if(ar[j]>ar[j+1])
             {
                 temp=ar[j];
                 ar[j]=ar[j+1];
                 ar[j+1]=temp;
             }
         }
    }
    cout<<"\n Sorted elements :";
    for(i=0;i<n;i++)
    cout<<ar[i]<<"\n";

    return 0;
}

 

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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