Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!

melanie bryant

Greenhorn
+ Follow
since Oct 26, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by melanie bryant

I'm havin difficulty with this class assignment. The code is writin down below. I have to place the largest number in the highest element of the array; after the second pass the 2 highest numbers, and so on. then instead of making nine comparisions on every pass make 8 on the second and so on.
Then I have to modify the sort to check at the end of each pass if any swaps have been made. If none have been made, the data must already be in the proper order, so the program should terminate. if the swaps have been made, at least one more pass is needed.

Any suggestions on getting started?



import java.awt.*;
import javax.swing.*;

public class exer7_11 extends JApplet {

// initialize applet
public void init()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add( outputArea );

int array[] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

String output = "Data items in original order\n";

// append original array values to String output
for ( int counter = 0; counter < array.length; counter++ )
output += " " + array[ counter ];

bubbleSort( array ); // sort array

output += "\n\nData items in ascending order\n";

// append sorted\ array values to String output
for ( int counter = 0; counter < array.length; counter++ )
output += " " + array[ counter ];

outputArea.setText( output );

} // end method init

// sort elements of array with bubble sort
public void bubbleSort( int array2[] )
{
// loop to control number of passes
for ( int pass = 1; pass < array2.length; pass++ ) {

// loop to control number of comparisons
for ( int element = 0;
element < array2.length - 1;
element++ ) {

// compare side-by-side elements and swap them if
// first element is greater than second element
if ( array2[ element ] > array2[ element + 1 ] )
swap( array2, element, element + 1 );

} // end loop to control comparisons

} // end loop to control passes

} // end method bubbleSort

// swap two elements of an array
public void swap( int array3[], int first, int second )
{
int hold; // temporary holding area for swap

hold = array3[ first ];
array3[ first ] = array3[ second ];
array3[ second ] = hold;
}

} // end class Exer7_11
17 years ago