/*
*Program:Selection Sort Algorithm
*Author:Joshua Dean Thompson
*Language: C, C++
*Compilier: DJGPP
*/
/* Function Prototypes */
void SelectionSort( int Array[], const int Size );
void PrintArray( int Array[], const int Size );
int main( void )
{
int i;
const int Size = 20;
int Array[ Size ];
/* Fill the Array with random values 0-99 */
for( i = 0; i < Size; i++ )
Array[i] = random() % 100;
/* Print the Random Array to Screen */
clrscr();
printf( "The Array with random order:\n\n");
PrintArray( Array, Size );
/* Wait for key Press... */
printf( "\nPress any key..." );
getch();
/* Sort the Array using Selection Sort */
SelectionSort( Array, Size );
/* Print the Smallest-to-Largest Order Array */
clrscr();
printf( "The Array after Selection Sort:\n\n");
PrintArray( Array, Size );
/* End the Program */
printf( "\nPress any key to quit..." );
getch();
return 0;
}
/* Uses the classic Selction sort algorithm */
void SelectionSort( int Array[], const int Size )
{
int i, j, smallest, temp;
for( i = 0; i < Size; i++ )
{
smallest = i;
for( j = i; j < Size; j++ )
{
if( Array[smallest] > Array[j] )
{
smallest = j;
}
}
temp = Array[i];
Array[i] = Array[smallest];
Array[smallest] = temp;
}
}
/* Prints an integer Array line by line */
void PrintArray( int Array[], const int Size )
{
int i;
for( i = 0; i < Size; i++ )
printf("Array[%i] = %i\n", i, Array[i] );
}