Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #define DEBUG 0
- void sortArray( int *a, int length );
- void printArray( int *a, int length );
- void arraySmallestValue( int value, int index );
- int main( int argc, char **argv )
- {
- int a[5] = {3, 5, 1, 4, 2};
- sortArray( a, 5 );
- return 0;
- }
- void sortArray( int *a, int length )
- {
- int i = 0;
- int smallest = 0;
- int index = 0;
- int replaceInd = 0;
- int replaceValue = 0;
- printArray( a, length ); // Starting array
- if ( 0 < length )
- {
- smallest = a[ 0 ];
- }
- arraySmallestValue( smallest, replaceInd );
- while ( ( length - 1 ) != replaceInd )
- {
- for ( i = replaceInd; i < length; i++ )
- {
- if ( a[ i ] < smallest )
- {
- smallest = a[ i ];
- index = i;
- }
- }
- if ( a[ replaceInd ] != smallest )
- {
- replaceValue = a[ replaceInd ];
- a[ replaceInd ] = smallest;
- a[ index ] = replaceValue;
- }
- replaceInd++;
- smallest = a[ replaceInd ];
- // DEBUGing part
- printArray( a, length );
- arraySmallestValue( smallest, replaceInd );
- }
- }
- void printArray( int *a, int length )
- {
- int i = 0;
- printf( "Here is array:\n" );
- for ( i = 0; i < length; i++ )
- {
- printf( "%d\t=>\t%d\n", i, a[ i ] );
- }
- }
- void arraySmallestValue( int value, int index )
- {
- printf( "We start with number %d and it is on index %d\n\n", value, index );
- }
Add Comment
Please, Sign In to add comment