For theoretical reference you can see wiki link.
http://en.wikipedia.org/wiki/Bubble_sort
For programmatic reference you can check this.
public class BubbleSort {
public static void main(String[] args) {
int arr[] = { 2, 7, 4, 1, 5, 3};
showAll(bubbleSort(arr));
}
private static int [] bubbleSort(int arr[]){
while(true){
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
count ++;
}
}
if(count == 0)
break;
}
return arr;
}
private static void showAll(int[] arr){
for (int i : arr) {
System.out.println("" + i);
}
}
}
No comments:
Post a Comment