using bubbleSort() and swap()


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: using bubbleSort() and swap()

  1. #1
    Join Date
    Mar 2003
    Posts
    15

    using bubbleSort() and swap()

    Here's this method in my program and it works but I wanted to make changes from this part to calling the swap method.

    For example:

    // sort components of array with bubble sort
    public void bubbleSort( int values[] ) {

    // loop to control number of passes
    for( int pass = 1; pass < values.length; pass++ )

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

    // compare side-by-side components and swap them if first
    // component is greater than second component
    if( values[ component ] > values[ component + 1 ] ) {
    // swap( values, component, component + 1 );
    int temp = values[ component ];
    values[ component ] = values[ component + 1 ];
    values[ component + 1 ] = values[ component ];
    } // end if

    } // end bubbleSort
    ----------------------------------------



    Using this:
    swap( values, component, component + 1 ); in the bubbleSort method to call for swap method....

    public void bubbleSort( int values[] ) {

    for( int pass = 1; pass < values.length; pass++ ) {

    for( int component = 0; component < values.length - 1;
    component++ ) {

    if( values[ component ] > values[ component + 1 ] )
    swap( values, component, component + 1 );

    } // end loop to control comparisons

    } // end loop to control passes
    } // end bubbleSort


    This bubbleSort calls the method swap to exchange the two components with this below:

    swap two components of an array
    public void swap( int values2[], int first, int second ) {

    int temp; // temporary holding area for swap

    temp = values2[ first ];
    values2[ first ] = values2[ second ];
    values2[ second ] = temp;

    } // end method swap

  2. #2
    Join Date
    Mar 2003
    Posts
    15
    when I made changes.. there are couple of errors relating to swap method. One of them is void in the public void swap method statement.

    Anyone have idea what the problem is?

  3. #3
    Join Date
    Mar 2003
    Posts
    834
    It helps if you say exactly what these errors are.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center