Bubble Sort Program in Java
Bubble Sort Program in Java to sort numbers of an array in ascending or descending order.
import java.util.*;
public class BubbleSort
{
public static void main(String[] args)
{
int temp;
int ar[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 9 - i; j++)
{
if (ar[j] > ar[j + 1])
{
temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
System.out.println("Sorted order:");
for (int i = 0; i < 10; i++)
{
System.out.println(ar[i]);
}
}
}
Output:
Enter the number ar[0]:12 Enter the number ar[1]:54 Enter the number ar[2]:21 Enter the number ar[3]:36 Enter the number ar[4]:25 Enter the number ar[5]:14 Enter the number ar[6]:47 Enter the number ar[7]:58 Enter the number ar[8]:69 Enter the number ar[9]:85 Sorted order: 12 14 21 25 36 47 54 58 69 85java java tutorials learn java study java