You are on page 1of 12

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 {

public static void bubbleSort(int [] sort_arr, int len){

for (int i=0;i<len-1;++i){

for(int j=0;j<len-i-1; ++j){

if(sort_arr[j+1]<sort_arr[j]){

int swap = sort_arr[j];


sort_arr[j] = sort_arr[j+1];
sort_arr[j+1] = swap;

}
}
}
}
public static void main( String args[] ) {
int [] array = {10,5,3,1,24,12};
int len = array.length;
bubbleSort(array,len);

for(int i = 0; i<len; ++i){


System.out.print(array[i] + " ");
}
}
}
Understanding implementation
The first step is to create a method, bubbleSort, that takes the array as the input to be sorted,
sort_arr, and the length of the array, len, as seen on line 3 in the code above.

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;

public class ExArraySort


{
public static void main(String args[])
{
// initialize the objects.
int n, i, j, temp;
int[ ] arr = new int[50];
Scanner scan = new Scanner(System.in);

// enter number of elements to enter.


System.out.print("Enter number for the array elements : ");
n = scan.nextInt();

// 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

This is a 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:

Enter no. of elements you want in array:5


Enter all the elements:
2
3
5
1
4
Descending Order:5,4,3,2,1
Sorting Array of String
import java.util.Arrays; for (int j = i+1; j<countries.length; j++)
public class SortStringArrayExample1 {
//compares each elements of the array to all the
{ remaining elements
public static void main(String args[]) if(countries[i].compareTo(countries[j])>0)
{ {
//swapping array elements
//defining an array of type String String temp = countries[i];
String[] countries = {"Zimbabwe", "South-Africa", "India", countries[i] = countries[j];
"America", "Yugoslavia", " Australia", "Denmark", "France", countries[j] = temp;
"Netherlands", "Italy", "Germany"}; }
int size = countries.length; }
}
//logic for sorting //prints the sorted array in ascending order
for(int i = 0; i<size-1; i++) System.out.println(Arrays.toString(countries));
{ }
}

You might also like