aspose file tools*
The moose likes Java in General and the fly likes Large arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Large arrays" Watch "Large arrays" New topic
Author

Large arrays

melanie bryant
Greenhorn

Joined: Oct 26, 2004
Posts: 1
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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Perhaps you can start with your first statement:

I have to place the largest number in the highest element of the array.

What actions do you need to take to do this? Can you list out a step by step procedure that can accomplish this task?

Think about this a bit and post back any ideas you have come up with. We'll be glad to help you from there.

Layne


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Large arrays