This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Large arrays

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic