Example of Bubble Sort in Java
Below is the java source code on sorting numbers using bubble sort . You can decide on how to sort a number either ascending or descending by just changing the comparison operator in the method BubbleSort.There are many sorting algorithms on sorting numbers but the most used is the selection sort. Actually, bubble sort is not a better choice on sorting numbers but it is pretty good to program while learning different sorting algorithms as well as recursion. It exposed the programmers on different ways on how to solve and program a certain problem and widen his/her knowledge as well as be able to compare its usefulness. After this program I will also upload the selection sort.
Updated:
Below is the quick link for the selection sort.
Java Source Code: How to Sort Numbers using Selection Sort in Recursion
source:linux-mag.com
How Bubble Sort Works?
Bubble sort sorts numbers by comparing the number and its following number. For example we will sort the entered 6 numbers in ascending way and here is the user’s input.
2 5 3 4 1 6
The first number is 2 and the following number is 5. Is 2 < 5? if yes, proceed. The second comparison will be 5 and 3. Is 5 < 3? since the answer is no, it will sort the numbers in this way.
2 3 5 4 1 6
Java Tutorials and Tips
Complete List of Java Tutorial and Source Codes for Absolute Beginners
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Class: Learn More About Classes in Java
Then, the next comparison is 5 and 4 and so on until it will be sorted properly from lowest to highest. Here is the complete list on how the numbers above sorted in bubble sort.
2 3 4 5 1 6
2 3 4 1 5 6
2 3 1 4 5 6
2 1 3 4 5 6
1 2 3 4 5 6 – end of sorting and this correct sorted numbers is what the program outputs to the screen
Below is the source code,
Java Source Code: How to sort numbers in Bubble Sort
//java class
public class BubbleSort
{
public void bubbleSort(int[] arr){
for(int i=0; i
Enter the size of the array: 10
Enter 10 numbers: 100 35 45 3 7 2 1 500 200 15
The Sorted Numbers: 1 2 3 7 15 35 45 100 200 500
Java Video Tutorials
Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99
Learn JAVA language -14h video & docs tutorials
Current Bid: $4.59
Java Books
Java Programming : From Problem Analysis to Program Design by D. S. Malik…
Current Bid: $2.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80
Other Java Source Code Examples
Java Simple Codes for Beginners
A Java source code: How to Output Asterisk in Different Forms Using Recursion
Java Source Code: A Recursive Asterisk Diamond Shape
Java Source code: How to Add Numbers inside an Array using For Loop
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code: Sort Numbers using Selection Sort
Java Source Code on a Recursive Linear Search
Java Source Code: Binary Search in Recursion
Java Source code: How to Print a String Backward using Recursion
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java source code: How to Output the Answer of the Integer X to the Power Y
Java Source Code: How to make a Program that will determine a Person’s Salutation and Current Age
Java Source Code: Recursive Snow Flakes