Design a class called BubbleSort that is similar to the class SelectionSort. The class BubbleSort will be used in the same way as the class SelectionSort, but it will use the bubble sort algorithm, which is
for(int i = 0; i < a.length-1; i++)
if(a[i]>a[i+1])
interchange the values of a[i] and a[i+1]
The bubble sort algorithm checks all adjacent pairs of elements in the array from the beginning to the end and interchanges any two elements that are out of order. This process is repeated until the array is sorted.
This is my code so far
public class BubbleSort
{
public void sort(double[] a, int n)
{
for(int i = 0; i < n-1; i++)
{
if(a[i] > a[i+1])
{
double temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
}
//driver program
public class BubbleSortDemo
{
public static void main(String[] args)
{
double[] a = {3.3, 4.2, 2.6, 5.8, 5.9};
BubbleSort good = new BubbleSort();
System.out.println("Numbers before sorting.");
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
good.sort(a, a.length);
System.out.println("Sorted array values.");
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
any suggestions on what else i have to do
thank you for your help