Quantcast
Channel: Java Tutorials » Java
Viewing all articles
Browse latest Browse all 19

Bubble Sort Example in JAVA

$
0
0

package in.malliktalksjava;

/**
* @author malliktalksjava
*
*/
public class BubbleSortExample {

private static int[] input = { 4, 2, 9, 6, 23, 11, 44, 0 };

/**
* @param args
* main method to run the logic.
*/
public static void main(String[] args) {
bubbleSort(input);

}
/**
* @param array – input array which required to sort.
*
* logic to sort the elements
*/
public static void bubbleSort(int array[]) {
int count = array.length;
int var;
for (int temp = count; temp >= 0; temp–) {
for (int temp2 = 0; temp2 < count – 1; temp2++) {
var = temp2 + 1;
if (array[temp2] > array[var]) {
swapNumbers(temp2, var, array);
}
}
printNumbers(array);
}
}

/**
* @param var1
* @param var2
* @param array
* Swap two numbers
*/
private static void swapNumbers(int var1, int var2, int[] array) {

int temp;
temp = array[var1];
array[var1] = array[var2];
array[var2] = temp;
}

/**
* @param input – Sorted array
*
* Prints the sorted array into console.
*/
private static void printNumbers(int[] input) {

for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + “, “);
}
System.out.println(“\n”);
}
}

Other Useful Links:

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java


Filed under: JAVA Tagged: Bubble sort example program in java, BubbleSort Example program in java, Java, Java Example Programs, Java sample Programs, Java Sorting alogorithms, Java Tutorials, public static void main, Sample Java Program for Bubblesort, Sorting Algorithms in Java

Viewing all articles
Browse latest Browse all 19

Trending Articles