C++ Programs

NAME:TO SORT INTEGER ARRAY USING POINTERS



#include
#include
#include

void selectionSort( int * const, const int ); // prototype
void swap( int * const, int * const ); // prototype

int main()
{
clrscr();
const int arraySize = 10;
int a[10];// = { 3, 0, -5, 9, 22, 0, 14, 7 };

cout<<"Enter Elements"; for(int i=0;i<10;i++) cin>>a[i];

selectionSort( a, arraySize ); // sort the array

cout << "Integers in ascending order\n";

for ( int j = 0; j < arraySize; j++ )
cout << setw( 4 ) << a[ j ];

cout << endl;
return 0; // indicates successful termination
} // end main

// function to sort an array
void selectionSort( int * const array, const int size )
{
int smallest; // index of smallest element

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;

swap( &array[ i ], &array[ smallest ] );
}
}

// swap values at memory locations to which
// x and y point
void swap( int * const x, int * const y )
{
int temp = *x;
*x = *y;
*y = temp;
}
/*
OUTPUT:
Enter Elements
21
32
5
14
91
66
45
11
0
77
Integers in ascending order
0 5 11 14 21 32 45 66 77 91