[Data Structure] Bubble Sort

  1. Compare adjacent elements. If the first one is bigger than the second one, swap the two of them.
  2. 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.
  3. Repeat the above steps for all elements except the last one.
  4. 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;i for(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;i System.out.println(a [i]);
}
}

Leave a Comment

Your email address will not be published.