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

## Recommended Answers

Do a web search - BubbleSort is a very common algorithm. You won't have trouble finding hundreds of Java code examples. Compare them to what you have and you'll see what sections you need to implement. If you're still confused at that point, then come ask.

Write on paper a set like a { 3,7,-1,5 } and make Your for loop on that. Notice, that iterating only 1 time on whole set is not enough.

Most primitive version of bubble sort is making such iteration n-times. (where n is the number of elements)

## All 7 Replies

Do a web search - BubbleSort is a very common algorithm. You won't have trouble finding hundreds of Java code examples. Compare them to what you have and you'll see what sections you need to implement. If you're still confused at that point, then come ask.

Write on paper a set like a { 3,7,-1,5 } and make Your for loop on that. Notice, that iterating only 1 time on whole set is not enough.

Most primitive version of bubble sort is making such iteration n-times. (where n is the number of elements)

well im trying to run the driver program but i get an error message that says it cant find the symbol class BubbleSort in line 7 of the driver program
any ideas
im also finding many of the codes are using a nested for loop but i need to use the example i gave with the for and if statement in my class
how can i make my class with that example

Put declaration of Your class into other file, but in same package in project - then compiler will find it. (dunno where You have it now)

And as I said - this, what You have in first post is NOT bubble sort. It lacks 1 more 'for' loop which nests Your present loop.

i fixed the save thing
now the problem is that it doesnt put 2.6 before 3.3
any ideas

Well, I wrote why it won't work 2 times already so... ;/

Well, I wrote why it won't work 2 times already so... ;/

``````public class BubbleSort
{
public static void main(String[] args)
{
double[] a = {3.3, 4.2, 2.6, 5.8, 5.9};
BubbleSort bs = new BubbleSort();
System.out.println("Numbers before sorting.");
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();

bs.bubbleSort(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();
}

public void bubbleSort(double[] a, int n)
{
for(int i = 0; i < n-1; i++)
for (int ctr=0; ctr < n-1-i;ctr++)
{
if(a[ctr] > a[ctr+1])
{
double temp = a[ctr];
a[ctr] = a[ctr+1];
a[ctr+1] = temp;
}
}
}
}
``````

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.