- Compare adjacent elements. If the first one is bigger than the second one, swap the two of them.
- Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
- Repeat the above steps for all elements except the last one.
- Repeat the above steps for fewer and fewer elements each time until there is no pair of numbers to compare.
public class bubbleSort {
public bubbleSort(){
int a[]={49,38,65,97,76,13,27,49, 78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp=0;
for(int i=0;ifor(int j=0;j if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+ 1]=temp;
}
}
}
for(int i=0;iSystem.out.println(a [i]);
}
}