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

#include "stdafx.h"
#include<iostream.h>

int* SelectiveSort ( int size, int array [] );
void exchange (int array[], int a, int b);

int main(int argc, char* argv[])
{
int array[10];
int i=0;

 while (i < 10)
{
cout<<"Please enter a number."<<endl;
cin>>array[i];
i++;
}
		cout<<"Unsorted array:"<<endl;
		for(int k=0; k<10;k++)
               cout<<array[k]<<endl;

 //call the sorting function
 int* arr=SelectiveSort (i,array);

		cout<<"Sorted array:"<<endl;
		for(int n=0; n<10;n++)
               cout<<arr[n]<<endl;
return 0;
 }

int*  SelectiveSort (int size, int array[])
{

	 int current = 0;
	 int* pointer;
	 pointer=array;

              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 ++;

			  }
return pointer;


}


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

Demo Applet

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



Learn SQL Programming By Examples [Kindle Edition]2.99

Learn PHP Programming by Examples [Kindle Edition] $2.99

Learn Visual Basic 6.0 [Kindle Edition] $1.99

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

New-trip.com website source code

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