CS 150 Introduction to C++ Programming - Spring 1998 

[ Home | Lecture Notes| WebTutor | WebTutor Site Map]


Selection Sort


Sorting is one of the most common operations on computers.There are many methods of sorting. Sorting algorithms use arrays to store the values before and after they are sorted. Here we discuss a method called Selection Sort.
The basic strategy behind Selection Sort is the following:

Translating this strategy into an algorithm written in pseudocode:

for each location in the array
   find the next smallest element
   swap that element into its proper location

Here is an animation of the Selection Sort algorithm written in C++. Notice the use of reference parameters (Remember arrays are always passed by reference). In the variables box, reference parameters are shown by setting them equal to a reference (shown as the character '&') before the argument to which they refer.

 

Choose one when done