File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Large arrays

 
melanie bryant
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic