Hi, this code compiles ok without any errors, but I am trying to sort an array of 10 integers into order using pointers. But when the output is produced It is printing them out the same as the original order. I looked over my code and cannot for the life of me see what I have done wrong
Code:
#include <iostream>
// function prototypes
void selectionSort ( int *const, const int );
void swap ( int *const, int *const );
// main function ///////////////////////////////////////////////////////////////
//
int main ( void )
{
const int arraySize = 10;
int data[ arraySize ] = { 2, 3, 10, 7, 9, 1, 6, 4, 8, 5 };
std::cout << "Orginal array:\n\n";
for ( int i = 0; i < arraySize; i++ )
std::cout << data[ i ] << " ";
// send to function to sort array
selectionSort ( data, arraySize );
std::cout << "\n\nSorted array:\n\n";
for ( int j = 0; j < arraySize; j++ )
std::cout << data[ j ] << " ";
std::cin.get(); // freeze console window output
return 0; // indicate program terminated sucessfully
}
// function to sort the passed array
void selectionSort ( int *const array, const int size )
{
int smallest; // insez of smallest element
// loop over size -1 elements
for ( int i = 0; i < size - 1; i++ )
{
smallest = i; // first index of remaining array
// loop to find index of smallest element
for ( int index = i + 1; index < size; index++ )
{
if ( array[ index ] < array[ smallest ] )
smallest = index;
// pass address of each array element to awap function
swap ( &array[ i ], &array[ smallest ] );
}
}
}
// function to swap values at memoty locations
// to which element1Ptr and element2Ptr point
void swap ( int *const element1Ptr, int *const element2Ptr )
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}