import java.util.Arrays;
public class BubbleSort {
public static void main(String args[]) {
//testing our bubble sort method in Java
int[] unsorted = {32, 39,21, 45, 23, 3};
bubbleSort(unsorted);
//one more testing of our bubble sort code logic in Java
int[] test = { 5, 3, 2, 1};
bubbleSort(test);
}
/*
* In bubble sort we need n-1 iteration to sort n elements
* at end of first iteration larget number is sorted and subsequently numbers smaller
* than that.
*/
public static void bubbleSort(int[] unsorted){
System.out.println("unsorted array before sorting : " + Arrays.toString(unsorted));
// Outer loop - need n-1 iteration to sort n elements
for(int i=0; i<unsorted.length -1; i++){
//Inner loop to perform the comparison and swapping between adjacent numbers
//After each iteration one index from last is sorted
for(int j= 1; j<unsorted.length -i; j++){
//If the current number is greater than swap those two
if(unsorted[j-1] > unsorted[j]){
int temp = unsorted[j];
unsorted[j] = unsorted[j-1];
unsorted[j-1] = temp;
}
}
System.out.printf("unsorted array after %d pass %s: %n", i+1, Arrays.toString(unsorted));
}
}
}
Comments
Post a Comment