The Selection Sort Algorithms - C++ source code.

++

Precondition: the function accepts an unsorted array and integer size that is the size of the array.

Post condition: Sorted array

The function looks for the smallest number and moves it to the head of the array













void  SelectiveSort ( int  size, int array [] )
{
     int current  = 0;

                while (  current <  size )
               {
                    int smallest = current;
                    int walker = current +1;

                        while ( walker < size )
                        {
                        if ( array [walker] < array [smallest] )
                                smallest = walker;
                        walker ++;
                        }
                    exchange ( array, current,  smallest );
                    current ++;
               }
}

void exchange (int array[], int a, int b)
{
           int temp=array[a];
           array[a]=array[b];
           array[b]=temp;
}

Demo Applet

Did you find information useful?
Send to your friend a link to this page

If you like this page click +1 button.

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



How to Build Your Own Web Site from Scratch [Kindle Edition] $2.99

Earn Money on Internet as an Affiliate [Kindle Edition] $0.99

Comments
 
Register to add comments ( 1000 char ) for selsortcpp.php.