Bubble Sort in Java Array
Bubble Sort in Java Array
Bubble Sort
• A simple sorting algorithm that compares
adjacent elements of an array and swaps them if
the element on the right is smaller than the one
on the left.
• It is an in-place sorting algorithm i.e. no extra
space is needed for this sort, the array itself is
modified.
class sorting {
if(sort_arr[j+1]<sort_arr[j]){
}
}
}
}
public static void main( String args[] ) {
int [] array = {10,5,3,1,24,12};
int len = array.length;
bubbleSort(array,len);
The second step is to create an outer for loop which will iterate over each element of the array
as shown on line 5.
The third step is to create an inner for loop as shown on line 7. This for loop starts from the first
element of the array till the second last index, (len - i - 1).
Each time one index less than the last is traversed as at the end of each iteration, the largest
element for that iteration reaches the end.
Within the nested for loop is the if condition from lines 9 to 15. This checks that if the element
onNote:
the left is greater than that on the right. If so, it swaps the two elements.
The outer loop will iterate over all elements in the array even if the array is already completely
sorted
Java program to sort a one dimensional array in ascending
order
Here, we are implementing a java program to sort an array (one-dimensional array) in
ascending order? We are reading array elements, arranging them in ascending order and
printing the sorted array.
Given an array and we have to sort its elements in ascending order and print the sorted
array using java program.
Example:
Input:
Array (elements will be read in program): 25 54 36 12 48 88 78 95 54 55
Output:
Sorted List in Ascending Order :
12 25 36 48 54 54 55 78 88 95
import java.util.Scanner;
// enter elements.
System.out.println("Enter " +n+ " Numbers : ");
for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
}
// sorting array elements.
System.out.print("Sorting array : \n");
for(i=0; i<(n-1); i++) Output:
{ Enter number for the array elements : 10
for(j=0; j<(n-i-1); j++) Enter 10 Numbers :
{ 25
if(arr[j] > arr[j+1]) 54
{ 36
temp = arr[j]; 12
arr[j] = arr[j+1]; 48
arr[j+1] = temp; 88
} 78
} 95
} 54
System.out.print("Array Sorted Successfully..!!\n"); 55
// array in ascending order. Sorting array :
System.out.print("Sorted List in Ascending Order : \n"); Array Sorted Successfully..!!
for(i=0; i<n; i++) Sorted List in Ascending Order :
{ 12 25 36 48 54 54 55 78 88 95
System.out.print(arr[i]+ " ");
}
}
}
Java Program to Sort the Array in Descending Order
Enter size of array and then enter all the elements of that array. Now with
the help of for loop and temp variable we sort the array in descending
order.
Here is the source code of the Java Program to Sort the Array in
Descending Order. The Java program is successfully compiled and run on a
Windows system. The program output is also shown below:
import java.util.Scanner;
for (int j = i + 1; j < n; j++)
public class Descending_Order
{
{
if (a[i] < a[j])
public static void main(String[] args)
{
{
temp = a[i];
int n, temp;
a[i] = a[j];
Scanner s = new Scanner(System.in);
a[j] = temp;
System.out.print("Enter no. of elements you want in array:");
}
n = s.nextInt();
}
int[ ] a = new int[n];
}
System.out.println("Enter all the elements:");
System.out.print("Descending Order:");
for (int i = 0; i < n - 1; i++)
for (int i = 0; i < n; i++)
{
{
System.out.print(a[i] + ",");
a[i] = s.nextInt();
}
}
System.out.print(a[n - 1]);
for (int i = 0; i < n; i++)
}
{
}
Output: