Bubble Sort
/** This is a program to implement bubble sort
Author : V.S.Phanindra
*/
The bubble sort is the oldest and simplest sort in use. Unfortunately, it's also the slowest, it sort works by comparing each item in the list with the item next to it, and swapping them if required.
Pros: Simplicity and ease of implementation.
Cons: Horribly inefficient.
class BubbleSort
{
public static void main(String[] args)
{
int a[]={2,1,4,6,8,9,-1,4,3,2};
int temp;
System.out.println("This is the List of elements before Sorting:");
for(int k=0;k<a.length;k++)
System.out.println(a[k]);
for(int i=0;i<a.length;i++)
{
for(int j=i+1;j<a.length-1;j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} }
System.out.println("This is the sorted List of Elements:");
for(int j=0;j<a.length;j++)
System.out.println(a[j]);
}
}
Author : V.S.Phanindra
*/
The bubble sort is the oldest and simplest sort in use. Unfortunately, it's also the slowest, it sort works by comparing each item in the list with the item next to it, and swapping them if required.
Pros: Simplicity and ease of implementation.
Cons: Horribly inefficient.
class BubbleSort
{
public static void main(String[] args)
{
int a[]={2,1,4,6,8,9,-1,4,3,2};
int temp;
System.out.println("This is the List of elements before Sorting:");
for(int k=0;k<a.length;k++)
System.out.println(a[k]);
for(int i=0;i<a.length;i++)
{
for(int j=i+1;j<a.length-1;j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} }
System.out.println("This is the sorted List of Elements:");
for(int j=0;j<a.length;j++)
System.out.println(a[j]);
}
}