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.
-------------------------------------------------------------
#include <stdio.h>
#include <iostream>
using namespace std;
void bubbleSort(int *array,int length){
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(array[i]>array[j])
{
int temp=array[i]; array[i]=array[j];
array[j]=temp;
}
}
}
}
void printElements(int *array,int length){
int i=0;
for(i=0;i<10;i++)
cout<<array[i]<<endl;
}
int main()
{
int a[]={9,6,5,23,2,6,2,7,1,8}; bubbleSort(a,10); printElements(a,10); return(0);
}
No comments:
Post a Comment