/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kollmann_hw1;
/**
*
* @author Awe
*/
public class Kollmann_HW1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double number[] = {99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,
82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,
60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,
38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,
16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
double temp;
boolean fixed = false;
while(fixed == false){
fixed = true;
for (int i = 0; i < number.length - 1; i++) {
if (number[i] > number [i + 1]){
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
}
for(int i = 0; i<number.length; i++){
System.out.println(number[i]);
}
}
}
Count the number of swaps made (bubble sort)
Page 1 of 13 Replies - 8618 Views - Last Post: 04 October 2012 - 03:53 AM
#1
Count the number of swaps made (bubble sort)
Posted 04 October 2012 - 02:24 AM
I am stuck on a bubble sort problem. I understand what a bubble sort is and how it works, but the syntax is throwing me off. I have a very basic version of it up and running in a sense, but all it did was sort the numbers from lowest to highest(i defined them highest to lowest in the array, so I know it is at least sort of working). But I can not for the life of me figure out how to code it to count the number of swaps made.
Replies To: Count the number of swaps made (bubble sort)
#2
Re: Count the number of swaps made (bubble sort)
Posted 04 October 2012 - 03:46 AM
elektrogeist, on 04 October 2012 - 02:54 PM, said:
I am stuck on a bubble sort problem. I understand what a bubble sort is and how it works, but the syntax is throwing me off. I have a very basic version of it up and running in a sense, but all it did was sort the numbers from lowest to highest(i defined them highest to lowest in the array, so I know it is at least sort of working). But I can not for the life of me figure out how to code it to count the number of swaps made.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kollmann_hw1;
/**
*
* @author Awe
*/
public class Kollmann_HW1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double number[] = {99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,
82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,
60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,
38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,
16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
double temp;
boolean fixed = false;
while(fixed == false){
fixed = true;
for (int i = 0; i < number.length - 1; i++) {
if (number[i] > number [i + 1]){
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
}
for(int i = 0; i<number.length; i++){
System.out.println(number[i]);
}
}
}
In bubble sort,if the size of the array is N, then the number of passes is N-1. Maybe you can try including a swap method which calls the bubblesort algorithm and also create a counter to show how many times the swap method has been called.
For the method counter, you could try creating a static variable which increments each time the method is called and in the end, display the value of the static variable as the number of times swap has been called.
regards,
Raghav
This post has been edited by raghav.naganathan: 04 October 2012 - 03:51 AM
#3
Re: Count the number of swaps made (bubble sort)
Posted 04 October 2012 - 03:48 AM
Just count the number of times you enter that if
if (number[i] > number [i + 1]){
shouldn't be complicated
if (number[i] > number [i + 1]){
shouldn't be complicated
#4
Re: Count the number of swaps made (bubble sort)
Posted 04 October 2012 - 03:53 AM
Can you point to a place in your code where a swap is occurring? If so, isolate that code (it already is) and add a counter that tallies how many times a swap happens. You could also pull that code out a write a method as Raghav suggests, adding a counter to the swap method.
Edit: Or follow pbl's finger . . .
Edit: Or follow pbl's finger . . .
This post has been edited by GregBrannon: 04 October 2012 - 03:56 AM
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|