Bubble Sort Example in JAVA


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

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , , , , , , ,
Posted in JAVA
4 comments on “Bubble Sort Example in JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
April 2014
M T W T F S S
« Feb   May »
 123456
78910111213
14151617181920
21222324252627
282930  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 265,565 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100