SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Java program to bubble sort
Java program to bubble sort
Output:
Enter the number of elements to sort: 10
Enter the numbers to sort:
3 4 1 2 5 7 10 9 8 6
The Sorted Array is:
10 9 8 7 6 5 4 3 2 1
Implementation:
import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { /* Function to bubble sort the array Receive the array and for every pass of the array if, there is an element which is greater than the previous one, simply swap those numbers */ public static void sortArray(int[] array) { int n=array.length; for(int i=0;i
array[j]) { swapPair(array,i,j); } } } } /* Function to swap two numbers which are not ought to be in right place 1. Store the first value in temporary variable 2. Store the second value in first variable 3. Store the temporary value in first variable */ public static void swapPair(int[] array,int i,int j) { int temp; temp=array[i]; array[i]=array[j]; array[j]=temp; } public static void main (String[] args) throws java.lang.Exception { /* scanner class to get the input from the console or keyboard by user */ Scanner in = new Scanner(System.in); /* integer array declaration */ int[] array; int n; /* get the number of elements to sort */ System.out.println("Enter the number of elements to sort:"); n = in.nextInt(); /* allocate the size of the array */ array = new int[n]; /* get the numbers in the array */ System.out.println("Enter the numbers to sort:"); for(int i=0;i
PREVIOUS
NEXT
HOME