Selection Sort


Program Listing

SelectionSort.cpp

/************************************************************************
*                                                                       *
*   Program:    SelectionSort                                           *
*                                                                       *
*   Purpose:    To sort a list of numbers into ascending order          *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

void SelectionSort(int list[], int size);
void Swap(int &x, int &y);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To sort a list of numbers into ascending order          *
*                                                                       *
************************************************************************/

int main() {

//  Read a list of numbers

    const int maxSize = 100;
    int list[maxSize];
    
    cout << "Enter the size of the list (max of " << maxSize << "):  " << flush;
    int size;
    cin >> size;
    
    cout << "Enter a list of " << size << " integers:" << endl;
    
    for (int i = 0; i < size; i++)
        cin >> list[i];
    
//  Sort the list

    SelectionSort(list, size);
    
//  Print the sorted list

    cout << endl << "The sorted list:" << endl;
    for (int i = 0; i < size; i++)
        cout << list[i] << endl;
        
    return 0;
}
    
/************************************************************************
*                                                                       *
*   Function:   SelectionSort                                           *
*                                                                       *
*   Purpose:    To sort a list of numbers using the selection sort      *
*               algorithm                                               *
*                                                                       *
************************************************************************/

void SelectionSort(int list[], int size) {

//  For each position in the list

    for (int i = 0; i < size - 1; i++) {
        int pos = i;
    
    //  Find the minimum value value from that position to the end
    
        int min = list[i];
        for (int j = i + 1; j < size; j++)
            if (list[j] < min) {
                pos = j;
                min = list[j];
            }
    
    //  Swap that position with the minimum
    
        Swap(list[i], list[pos]);
    }
    return;
}

/************************************************************************
*                                                                       *
*   Function:   Swap                                                    *
*                                                                       *
*   Purpose:    To swap the values of two integers                      *
*                                                                       *
************************************************************************/

void Swap(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
    return;
}

Sample Run

Enter the size of the list (max of 100):  10
Enter a list of 10 integers:
40 30 60 75 45 50 25 90 70 35

The sorted list:
25
30
35
40
45
50
60
70
75
90



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:57 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/SelectionSort.html

Return to Coms 261 home page