Below is the demo of Bubble sort algorithm.
package com.javadevelopersnotes.algo;
import java.util.Arrays;
public class BubbleSortAlgo {
public static void main(String[] args) {
int[] input = {33, 21, 45, 64, 55, 34, 11, 8, 3, 5, 1};
System.out.println("Before Sorting : ");
System.out.println(Arrays.toString(input));
bubbleSort(input);
}
private static int[] bubbleSort(int[] list) {
int i, j, temp = 0;
for (i = 0; i < list.length - 1; i++) {
for (j = 0; j < list.length - 1 - i; j++) {
if (list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
System.out.println("After Sorting : ");
System.out.println(Arrays.toString(list));
return list;
}
}
No comments:
Post a Comment