Introduction
Bubble sort is the simplest sorting algorithm. It has the following steps
a) Iterates through the list
b) Compares the adjacent elements
c) Swap the adjacent elements if they are in not in proper sorting order
d) The list is traversed repeatedly unless the list is sorted
Worst Complexity: O(n*n)
Best Complexity : O(n)
Alternate name : Sinking Sort
Implementation using Simple Loops
Implementation using Java 8 Stream and lambda expressions
Input : 0,5,2,6,3,1
Output: 0 1 2 3 5 6 (Ascending)
Output: 6 5 3 2 1 0 (Descending)
References
https://en.wikipedia.org/wiki/Sorting_algorithm#Bubble_sort
Bubble sort is the simplest sorting algorithm. It has the following steps
a) Iterates through the list
b) Compares the adjacent elements
c) Swap the adjacent elements if they are in not in proper sorting order
d) The list is traversed repeatedly unless the list is sorted
Worst Complexity: O(n*n)
Best Complexity : O(n)
Alternate name : Sinking Sort
Implementation using Simple Loops
public class BubbleSortExample {
private static int array[] = {0,5,2,6,3,1};
static int temp = 0;
public static void main(String[] args) {
bubbleSortAscending();
System.out.println("Ascending Sorted List");
for(int value : array) {
System.out.print(value + " ");
}
bubbleSortDescending();
System.out.println("Descending Sorted List");
for(int value : array) {
System.out.print(value + " ");
}
}
private static void bubbleSortAscending() {
for(int outer=0; outer<array.length; outer++) {
for(int inner=1; inner < array.length-outer;inner++) {
//for ascending use Greater sign > for swap check
if(array[inner-1] > array[inner] ) {
temp = array[inner-1];
array[inner-1] = array[inner];
array[inner] = temp;
}
}
}
}
private static void bubbleSortDescending() {
for(int outer=0; outer<array.length; outer++) {
for(int inner=1; inner < array.length-outer;inner++) {
//for descending use lesser sign < for swap check
if(array[inner-1] < array[inner] ) {
temp = array[inner-1];
array[inner-1] = array[inner];
array[inner] = temp;
}
}
}
}
}
import java.util.Arrays;
import java.util.stream.IntStream;
public class BubbleSortJava8Example {
private static int array[] = {0,5,2,6,3,1};
static int temp = 0;
public static void main(String[] args) {
bubbleSortAscending();
System.out.println("Ascending Sorted List");
Arrays.stream(array).forEach(System.out::print);
bubbleSortDescending();
System.out.println();
System.out.println("Descending Sorted List");
Arrays.stream(array).forEach(System.out::print);
}
private static void bubbleSortAscending() {
IntStream.range(0, array.length).flatMap(outer -> IntStream.range(1, array.length - outer))
.forEach(inner -> {
if(array[inner-1] > array[inner]) {
int temp = array[inner-1];
array[inner-1] = array[inner];
array[inner] = temp;
}
});
}
private static void bubbleSortDescending() {
IntStream.range(0, array.length).flatMap(outer -> IntStream.range(1, array.length - outer))
.forEach(inner -> {
if(array[inner-1] < array[inner]) {
int temp = array[inner-1];
array[inner-1] = array[inner];
array[inner] = temp;
}
});
}
}
Output: 0 1 2 3 5 6 (Ascending)
Output: 6 5 3 2 1 0 (Descending)
References
https://en.wikipedia.org/wiki/Sorting_algorithm#Bubble_sort
Comments
Post a Comment