16.Write a Program to implement Bubble sort using a 1-D array.
#include<iostream.h>
void Bubblesort(int[ ], int);
int main()
{
int AR[50],item, n, index;
cout<<"How many elements do u want to create array with?(max..50)..";
cin>>n;
cout<<"\n Enter Array elements ..\n";
for(int i=0;i<n;i++)
cin>>a[i];
Bubllesort(AR, n);
cout<<"\n \n The sorted array is as shown below \n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
void Bubblesort(int AR[], int size)
{
int t,ct=0;
for(int i=0; i<size; i++)
{
for(int j=0; j<(size-1); j++)
{
if(AR[j]>AR[j+1])
{
t=AR[j];
AR[j]=AR[j+1];
AR[j+1]=t;
}
}
cout<<"Array after iteration-"<<++ct<<"-is:";
for(int k=0;k<size;k++)
cout<<AR[k]<<" ";
cout<<endl;
}
}
#include<iostream.h>
void Bubblesort(int[ ], int);
int main()
{
int AR[50],item, n, index;
cout<<"How many elements do u want to create array with?(max..50)..";
cin>>n;
cout<<"\n Enter Array elements ..\n";
for(int i=0;i<n;i++)
cin>>a[i];
Bubllesort(AR, n);
cout<<"\n \n The sorted array is as shown below \n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
void Bubblesort(int AR[], int size)
{
int t,ct=0;
for(int i=0; i<size; i++)
{
for(int j=0; j<(size-1); j++)
{
if(AR[j]>AR[j+1])
{
t=AR[j];
AR[j]=AR[j+1];
AR[j+1]=t;
}
}
cout<<"Array after iteration-"<<++ct<<"-is:";
for(int k=0;k<size;k++)
cout<<AR[k]<<" ";
cout<<endl;
}
}
No comments:
Post a Comment