pdfcrowd.com open in browser PRO version Are you a developer?

Try out the HTML to PDF API
Home
Home Programming Books C Programming Tutorials C Graphics Source codes Submit Program Facebook
C Mouse Programs
C programming
examples
C Source codes
Java programs
graphics.h
C graphics programs
conio.h
math.h
dos.h
Java programming
source codes
Hello World
If else
For loop
While loop
Print Alphabets
Print Multiplication
Table
Get Input From User
Java program to bubble sort
Java program to bubble sort: This code sorts numbers inputted by user using
Bubble sort algorithm.
Java programming code
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
1
Addition
Find Odd or Even
Fahrenheit to celsius
Java Methods
Static Block
Static Method
Multiple classes
Java constructor tutorial
Java exception handling
tutorial
Swapping
Largest of three integers
Enhanced for loop
Factorial
Primes
Armstrong number
Floyd's triangle
Reverse String
Palindrome
Compare Strings
Linear Search
Binary Search
Substrings of string
Display date and time
Random numbers
Garbage Collection
IP Address
Reverse number
Add Matrices
Transpose Matrix
Multiply Matrices
Bubble sort
Open notepad
import java.util.Scanner;

class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);

System.out.println("Input number of integers to sort");
n = in.nextInt();

int array[] = new int[n];

System.out.println("Enter " + n + " integers");

for (c = 0; c < n; c++)
array[c] = in.nextInt();

for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

System.out.println("Sorted list of numbers");

for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
Complexity of bubble sort is O(n
2
) which makes it a less frequent option for
arranging in sorted order when quantity of numbers is high.
Download Bubble sort Java program.
Output of program:
► String Java
► Java Scanner
► Java Sort
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
22 Like
Tweet 1
► Java Programming
► Java Source Code
► Java Void
► Sorting Java
► Java C Code
► Java C++
You can also use sort method of Arrays class to sort integers in ascending order but
remember that sort method uses a variation of Quick sort algorithm.
import java.util.Arrays;

class Sort
{
public static void main(String args[])
{
int data[] = { 4, -5, 2, 6, 1 };

Arrays.sort(data);

for (int c: data)
{
System.out.println(c);
}
}
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Programming Simplified is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Home | About Us | Contact Us | Programmer Resources | Site Map | Privacy | Download Software
}
HP Chromebook 11
google.com/chromebook
Everything you need in one laptop.
Made with Google. Learn more.