Array

bubble sort

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,i,pntr,temp;
cout<<"\n ENTER THE NUMBER OF ELEMENTS=";
cin>>n;
int arr[n];
cout<<"\n ENTER THE VALUES=";

for(i=0;i<n;i++)
cin>>arr[i];

for(i=0;i<n;i++)
{
    pntr=1;
    while(pntr<=(n-i))
    {
        if(arr[pntr]>arr[pntr+1])
        {
            temp=arr[pntr];
            arr[pntr]=arr[pntr+1];     
            arr[pntr+1]=temp;
       
        }
    pntr+=1;
    }
}
cout<<"\n YOUR SORTED ARRAY IS:=\n";
for(i=0;i<n;i++)
cout<<arr[i]<<" ";

getch();
}

Post a Comment

Previous Post Next Post