I am amost done with the code but I need a little bit more help I need to add the bubble sort code & write exchange increment line
and add the selection sort code & write exchange increment line, but I don't know how to do it can anyone help me?

#include <iostream>
using namespace std;

// Constant for array sizes
const int SIZE = 20;

// Function prototypes
int bubbleSort(long [], int);  
int selectionSort(long [], int);

int main()
{
   int exchanges; // Number of exchanges made

   // Two arrays with identical values
   long accounts1[SIZE] =
      { 5658845,  4520125,  7895122,
        8777541,  8451277,  1302850,
        8080152,  4562555,  5552012,
        5050552,  7825877,  1250255,
        1005231,  6545231,  3852085,
        7576651,  7881200,  4581002 };
        
   long accounts2[SIZE] = 
      { 5658845,  4520125,  7895122,
        8777541,  8451277,  1302850,
        8080152,  4562555,  5552012,
        5050552,  7825877,  1250255,
        1005231,  6545231,  3852085,
        7576651,  7881200,  4581002 };
        
   // Sort accounts1 with bubble sort. The function
   // returns the number of exchanges made.
   exchanges = bubbleSort(accounts1, SIZE);
   
   // Display the number of exchanges made by the
   // bubble sort.
   cout << "\n" << exchanges 
        << " exchanges were made by Bubble Sort."
        << endl;

   // Sort accounts2 with selection sort. The function
   // returns the number of exchanges made.
   exchanges = selectionSort(accounts2, SIZE);
   cout << "\n" << exchanges 
        <<  " exchanges were made by Selection Sort." 
        << endl;

   return 0;
}

int bubbleSort(long array[], int size)
{
   bool swap;
   long temp;
   int  exchanges = 0;

   do
   {
      swap = false;   // No swaps made yet on this pass.

/*    *****
Copy the FOR loop for the bubble sort from the book.
Include one line to increment the number of exchanges
as the last line of the IF loop
*/





   } while (swap);  // Same as  while (swaps == true);

   return exchanges;
}


int selectionSort(long array[], int size)
{
   int  startScan,    // Array indexes to be compared
        minIndex,
        exchanges = 0;
   long minValue;
  

Copy the FOR loop from the book.
*/
    
      
      // Move values to their new positions
      array[minIndex]  = array[startScan];
      array[startScan] = minValue;
      // Add the line to increment the number of exchanges.
-->

   } 
   return exchanges;
}

The comment blocks say to copy the code from the book. Have you looked in your book for the bubble and exchange sort algorithms that your instructor wants you to use?

Once you have done that, it's as simple as adding the line exchanges++; to the appropriate places in the loops.

Edited 7 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.