Thread: Bubble Sort and Selection Sort

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8

    Bubble Sort and Selection Sort

    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:
    
    
    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; 
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. You need to prototype the functions before main(), before you try and call them.

    2. You need to make a copies of your array before passing it to each sort function.
    As written, your selection sort is being passed sorted data (hardly a fair test).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Since you're too dumb to learn the cross-posting message
    Bubble Sort And Selection Sort - C And C++ | Dream.In.Code

    Let me make it easier for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-19-2013, 08:24 AM
  2. Replies: 5
    Last Post: 04-03-2011, 02:05 PM
  3. Bubble Sort / selection problem
    By Drew in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 03:23 PM
  4. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM