Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3026

Bubble Sort and Selection Sort

$
0
0
You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.

You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.

I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861)

Also, I feel like I'm missing something in int main() to get it to sort properly. Any help is greatly appreciated.

Code:

# include <iostream>

using namespace std;

int main()

{
        const int SIZE1 = 3;
        const int SIZE2 = 8;
       
        int arr [SIZE1][SIZE2] = { { 105, 102, 107, 103, 106, 100, 104, 101 },
                                              { 108, 106, 105, 110, 111, 100, 101, 107 },
                                              { 112, 118, 104, 103, 111, 100, 102, 101 }
               
        };

        for (int i = 0; i <= SIZE1 - 1; i++)

        {
                bubbleSort(arr[i], SIZE2);

                cout << endl;

                selectionSort(arr[i], SIZE2);

                cout << endl;

               
       
}

        system("pause");
        return 0;
       
}

void bubbleSort (int arr[], int size)

{
        bool swap;
        int temp;

        do
        {
                swap = false;
                for (int count = 0; count < (size - 1) ; count ++)
                {
                        if (arr[count] > arr [count + 1])
                        {
                                temp = arr  [count];
                                arr [count] = arr [count + 1];
                                arr [count + 1] = temp;
                                swap = true;
                        }

                }

        } while (swap);

}

void selectionSort (int arr[], int size)
{
        int startScan, minIndex, minValue;
       
        for (startScan = 0; startScan < (size - 1); startScan++)

        {
                minIndex = startScan;
                minValue = arr[startScan];
                int index;
                for (index = startScan + 1; index < size; index++)
                {
                        if (arr[index] < minValue)

                        {
                                minValue = arr[index];
                                minIndex = index;
                        }
                }
                arr[minIndex] = arr[startScan];
                arr[startScan] = minValue;
        }
}


Viewing all articles
Browse latest Browse all 3026

Trending Articles