Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.
// Example Program in C++ // to sort an array
// using bubble sort
#include
void main(void)
{
int temp, i, j;
int ar[10];
cout<<"enter elements of array:\n";
for(i=0; i<10; i++)
cin>>ar[i];
// sorting is done here
for(i=0;i<10;i++)
for(j=0; j<(10-1); j++)
if (ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
// till here
for(i=0; i<10; i++)
cout<
cout<
No comments:
Post a Comment