PDA

View Full Version : Problems with bubbleSort


shadowls
09-02-2009, 11:17 PM
It should ask for numbers, show the presorted numbers, sort the numbers, and show the sorted numbers, total number of passed and total number of swaps. So far, I have it asking for the numbers and showing the presorted numbers and it stops there. Any help would be wonderful.

/* CIS 406 – Section 3016 – Java Programming I
Program 7 – Bubble Sort (based on Chapter 11)
Olivia Riggs
Aug 31, 09
*/

import javax.swing.*;

class Program7 {
public static void main (String[] args) {
double[] number = new double[9];


for (int i = 0; i < number.length; i++) {
number[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Input number: " + (i+1)));
}
for (int index = 0; index < number.length; index++) {
System.out.println("Your numbers are: " + number[index]);
}
}
public void bubbleSort( int[] number ) {
double swap = 0;
double pass = 0;
int temp, bottom;
boolean exchanged = true;

bottom = number.length - 2;

while (exchanged) {
exchanged = false;
for (int i = 0; i <= bottom; i++) {
if (number[i] > number[i+1]) {
temp = number[i];
number[i] = number[i+1];
number[i+1] = temp;
exchanged = true;
swap = swap + 1;
}
}
}
pass = pass + 1;

for (int index = 0; index < number.length; index++) {

System.out.println("Your numbers sorted are: " + number[index]);
System.out.println("Number of swaps: " + swap);
System.out.println("Number of passes: " + pass);
}
}
}



The output is:

----jGRASP exec: java Program7

Your numbers are: 23.0
Your numbers are: 17.0
Your numbers are: 5.0
Your numbers are: 90.0
Your numbers are: 12.0
Your numbers are: 44.0
Your numbers are: 38.0
Your numbers are: 84.0
Your numbers are: 77.0

----jGRASP: operation complete.

Thank you so much.

oracleguy
09-02-2009, 11:47 PM
Well your pass variable is always going to be 1 because it is outside the while loop. And the for loop at the end of the function probably shouldn't be printing the number of passes and swaps with each number.

You are also never calling your sort function from main.

Fix those things and then post what the output is from the program then.

shadowls
09-03-2009, 02:31 AM
Thank you so much. This is what it looks like now.

/* CIS 406 – Section 3016 – Java Programming I
Program 7 – Bubble Sort (based on Chapter 11)
Olivia Riggs
Aug 31, 09
*/

import javax.swing.*;

class Program7 {
public static void main (String[] args) {
double[] number = new double[9];


for (int i = 0; i < number.length; i++) {
number[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Input number: " + (i+1)));
}
for (int index = 0; index < number.length; index++) {
System.out.println("Your numbers are: " + number[index]);
}
bubbleSort(number);
}
public static void bubbleSort( double[] number ) {
double swap = 0;
double pass = 0;
double temp, bottom;
boolean exchanged = true;

bottom = number.length - 2;

while (exchanged) {
exchanged = false;
for (int i = 0; i <= bottom; i++) {
if (number[i] > number[i+1]) {
temp = number[i];
number[i] = number[i+1];
number[i+1] = temp;
exchanged = true;
swap = swap + 1;
}
}
pass = pass + 1;
}


for (int index = 0; index < number.length; index++) {

System.out.println("Your numbers sorted are: " + number[index]);
}
System.out.println("Number of swaps: " + swap);
System.out.println("Number of passes: " + pass);
}
}

The output looks like this:


----jGRASP exec: java Program7

Your numbers are: 23.0
Your numbers are: 17.0
Your numbers are: 5.0
Your numbers are: 90.0
Your numbers are: 12.0
Your numbers are: 44.0
Your numbers are: 38.0
Your numbers are: 84.0
Your numbers are: 77.0
Your numbers sorted are: 5.0
Your numbers sorted are: 12.0
Your numbers sorted are: 17.0
Your numbers sorted are: 23.0
Your numbers sorted are: 38.0
Your numbers sorted are: 44.0
Your numbers sorted are: 77.0
Your numbers sorted are: 84.0
Your numbers sorted are: 90.0
Number of swaps: 12.0
Number of passes: 4.0

----jGRASP: operation complete.

Fou-Lu
09-03-2009, 03:26 AM
Looks ok sofar, except you're probably wanting to sort before output.

shadowls
09-05-2009, 11:55 AM
I need the begining list of numbers befor sort and after sort.

Fou-Lu
09-05-2009, 10:55 PM
I need the begining list of numbers befor sort and after sort.

Ah sorry, I didn't notice that you had the two sets of output, I was only paying attention to the unsorted.
The only thing I would change if I were you is to do all the display from the main rather than the sort.

Old Pedant
09-06-2009, 06:22 AM
As a comment, only:

Even for a bubble sort, that version is not very efficient. Because it has to scan the entire array every time through the while(exchanged) loop.

If you think about it, you are *guaranteed* that the highest number will have "bubbled" to the last array spot first time through the loop. So there's no need to loop to the last slot on the 2nd time through. And so on. That is, the number of elements that need to be checked for swaps is lower by one after each time through that outer "while" loop.

So can you think of an easy way to improve the performance? Hint: add one line to your code.

[It's still an "order n-squared" algorithm, the worst kind, but on average it will be nearly twice as fast as what you have now.]