here is my codes
basicallysorting works
but now i have to set a counter to display out the contents of array1 and array2 each pass of the sort. the problem is where to put that counter????

#include <iostream>
#include <iomanip>

using namespace std;
// Function prototypes
void showArray1(int [], int);
void bubbleSortArray1(int [], int);
void showArray2(int [], int);
void selectionSortArray2(int [], int);

 int main()
 {
 const int SIZE = 8;  //size of the arrays
 //two identical arrays of 8 integers 
 
 int array1[SIZE] = {7, 2, 3, 8, 9, 1, 5, 4};
 int array2[SIZE] = {1, 5, 7, 8, 9, 3, 2, 0};  //??
 
 //display First array
 cout << "The unsorted values in array1 are:\n";
showArray1(array1, SIZE);
  
 //*call the function to sort array 1[ Binary sort]
bubbleSortArray1(array1, SIZE);
cout << "The Bubble sorted values of array1 are:\n";
showArray1(array1, SIZE);
  
 //display second array
 cout << "The unsorted values in array2 are:\n";

 showArray2(array2, SIZE);
 
//call the function to selection sort array2 in ascending order
 
selectionSortArray2(array2, SIZE);
cout << "The Selection sorted values of array2 are:\n";

showArray2(array2, SIZE);

 cout << "End of Program.\n";
   system("PAUSE");
	return 0;
}
/**********Function to display array1***************/
void showArray1(int array1[], int elems)
{
for (int count = 0; count < elems; count++)
cout << array1[count] << " ";
cout << endl;
}
  /**********Function to display array2***************/
void showArray2(int array2[], int elems)
{
     for (int count = 0; count < elems; count++)
         cout << array2[count] << " ";
         cout << endl;
}
//**********Function to BUBBLE sort array1***************/
//***********Modified to print elements position after each pass*****/
/***********************************************************
This function performs an ascending-order bubble sort on *
 array1. The parameter elems holds the number of elements *
in the array. *
***********************************************************/
void bubbleSortArray1(int array[], int elems)
{
          int temp;
          bool swap;
     do
     { swap = false;
            for (int count = 0; count < (elems - 1); count++)
            {
               if (array[count] > array[count + 1])
                  {
                    temp = array[count];
                    array[count] = array[count + 1];
                    array[count + 1] = temp;
                    swap = true;
                    }
 
            }
     } while (swap);
} 
//**********Function to selection sort array2***************/
//***********Modified to print elements position after each pass*****/
void selectionSortArray2(int array[], int elems)
{
     int startScan, minIndex, minValue;

      for (startScan = 0; startScan < (elems - 1); startScan++)
      {
          minIndex = startScan;
          minValue = array[startScan];
             for (int index = startScan + 1; index < elems; index++)
                 {
                 if (array[index] < minValue)
                    {
                     minValue = array[index];
                     minIndex = index;
                     }
                  }
          array[minIndex] = array[startScan];
          array[startScan] = minValue;
       }
}

Dani AI

Generated

Short answer: print the array at the end of each outer pass (not after the whole sort). A "pass" is one full run of the inner loop; a "swap" is a single element exchange. For bubble sort put the counter and the print call immediately after the inner loop completes (inside the outer loop or the do/while). For selection sort print immediately after the pass swap that places the minimum into the current position. Replace the two nearly identical showArray1/showArray2 functions with one print function and call it from both sorts.

Example (illustrates where to place the print call):

void printArray(const int a[], int n) {
    for (int i = 0; i < n; ++i) cout << a[i] << ' ';
    cout << '\n';
}

void bubbleSort(int a[], int n) {
    for (int pass = 1; pass <= n-1; ++pass) {
        bool swapped = false;
        for (int i = 0; i < n - pass; ++i) {
            if (a[i] > a[i+1]) { std::swap(a[i], a[i+1]); swapped = true; }
        }
        cout << "Pass " << pass << ": ";
        printArray(a, n);            // <-- snapshot after this pass
        if (!swapped) break;         // early exit if already sorted
    }
}

And for selection sort, print after the min is placed:

void selectionSort(int a[], int n) {
    for (int pass = 1; pass <= n-1; ++pass) {
        int minIdx = pass-1;
        for (int j = pass; j < n; ++j) if (a[j] < a[minIdx]) minIdx = j;
        if (minIdx != pass-1) std::swap(a[minIdx], a[pass-1]);
        cout << "Pass " << pass << ": ";
        printArray(a, n);            // <-- snapshot after placing the min
    }
}

Notes and troubleshooting:

  • As pointed out, printing on every swap produces a finer-grained trace; printing after each pass gives one snapshot per pass (much less output for bigger arrays).
  • Use '\n' or endl to force a newline. If nothing appears, confirm the print call is inside the outer loop (not after it), and that the program actually reaches that code path.
  • For large arrays, limit printing (or print only selected passes) to keep the trace readable.

Recommended Answers

All 3 Replies

In bubble sort when every you swap print out the array.

In selection sort, whenever you swap print out the array.

Also your functions : showArray1 and showArray2 is repetitive to each
other. You only need one of them, and pass array1 or array2 whenever
you wan't to print them to one of the function. So keep showArray1 or
showArray2 and discard the other.

but its not showing/displaying any thing on creen

void bubbleSortArray1(int array[], int elems)
{
          int temp;
          bool swap;
  // counts number of modifications made to elements 
		int pass = 0;
     do
     { swap = false;
            for (int count = 0; count < (elems - 1); count++)
            {
               if (array[count] > array[count + 1])
                  {
                    temp = array[count];
                    array[count] = array[count + 1];
                    array[count + 1] = temp;
                    swap = true;
                    }
 
            }
     } while (swap);
     cout<<"pass"<<pass++;
}

Actually counter is working now, but how do I display the array after each modification of elements in Bubble sort??????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.