UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

Selection Sort

The Task

With arrays, we can sort a list of integers entered by the user. This program uses an algorithm called "Selection Sort" to put the numbers in increasing order.

The Program

/**************************************** ** File: sort.c ** Author: Richard Chang ** Date Written: ? ** Modified by: Sue Bogar ** Modification date: 2/25/98 ** Section: 101 ** SSN: 123-45-6789 ** EMail: bogar@cs.umbc.edu ** Sort a list of numbers using an array ** and selection sort. *****************************************/ #include <stdio.h> /* sentinel input value */ #define SENTINEL -1 /* max number of values to be sorted */ #define MAX 100 /* function prototypes */ int ReadArray (int a[], int maxSize) ; void SelectionSort (int a[], int size) ; int FindSmallest (int a[], int start, int stop) ; int main ( ) { int a[MAX], items, i ; printf("Enter items, one per line.\n") ; printf("End with sentinel: %d\n", SENTINEL) ; items = ReadArray(a, MAX) ; if (items == -1) { printf("Too many items!!!\n") ; } else { SelectionSort(a, items) ; printf("\nSorted list:\n") ; for (i = 0; i < items; i++) { printf("a[%d] = %d\n", i, a[i]) ; } } return 0; } /********************************************* ** Function: SelectionSort ** Input: an array of ints to be sorted ** the size of the array ** Output: the array is sorted "in place" ** there is no return value ** Description: ** Selection sort finds the smallest value ** in the unsorted portion of the array and ** moves it into the current position. The ** values "swap" positions. **********************************************/ void SelectionSort(int a[], int size) { int unsorted, smallestIndex, temp ; for (unsorted = 0; unsorted < size; unsorted++) { smallestIndex = FindSmallest(a, unsorted, size); /* swap values */ temp = a[smallestIndex] ; a[smallestIndex] = a[unsorted] ; a[unsorted] = temp ; } } /********************************************* ** Function: FindSmallest ** Input: an array of ints to search ** the 'start'ing and 'stop'ping indices in ** the array between which to search for ** the smallest ** Output: returns the smallest value ** between a[start] and a[stop - 1] **********************************************/ int FindSmallest(int a[], int start, int stop) { int smallestValue, smallestIndex, i ; smallestIndex = start ; smallestValue = a[start] ; for (i = start+1 ; i < stop ; i++) { if (a[i] < smallestValue) { smallestIndex = i ; smallestValue = a[i] ; } } return (smallestIndex) ; } /********************************************* ** Function: ReadArray ** Input: an array of ints in which to store values ** the size of the array ** Output: values are stored the array ** returns the number of elements stored in ** the array or returns -1 if too many ** values are read **********************************************/ int ReadArray(int a[], int maxSize) { int i, value ; i = 0 ; /* The priming read */ scanf ("%d", &value); while (value != SENTINEL) { if (i == maxSize) { return(-1) ; } a[i] = value ; i++ ; scanf ("%d", &value); } return (i) ; }

The Sample Run

linux1[74] % a.out Enter items, one per line. End with sentinel: -1 10 8 6 2 7 1 3 -1 Sorted list: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 6 a[4] = 7 a[5] = 8 a[6] = 10 linux1[75] % cat data 8 12 7 13 32 6 29 1 18 10 2 17 1 3 30 linux1[76] % a.out < data Enter items, one per line. End with sentinel: -1 Sorted list: a[0] = 1 a[1] = 1 a[2] = 2 a[3] = 3 a[4] = 6 a[5] = 7 a[6] = 8 a[7] = 10 a[8] = 12 a[9] = 13 a[10] = 17 a[11] = 18 a[12] = 29 a[13] = 30 a[14] = 32 linux1[77] %
Last Modified - Thursday, 17-Jan-2002 13:52:11 EST


CSEE | 201 | 201 S'02 | lectures | news | help