# include # include int iran ( int a, int b, int *seed ); int main ( ) { int i; int j; int seed; int x[10]; int t; // // Set the array to random values between 0 and +100. // seed = time ( 0 ); for ( i = 0; i < 10; i++ ) { x[i] = iran ( 0, 100, &seed ); } // // Print the array. // printf ( "\n" ); printf ( "Initial array:\n" ); printf ( "\n" ); for ( i = 0; i < 10; i++ ) { printf ( "x[%i] = %i\n", i, x[i] ); } // // Think of the partial array from A[0] to A[I], assuming // that A[I] has just been included, and the preceding elements are sorted. // for ( i = 1; i < 10; i++ ) { // // Compare the value in A[I] to the value in A[I-1]. // If a swap is necessary, we next have to compar A[I-1] to A[I-1] and so on. // for ( j = i; 0 < j; j-- ) { // // If the next neighbor is smaller, then all is in order. // if ( x[j-1] < x[j] ) { break; } // // Otherwise, the I-th value swaps with its neighbor. // and must get ready to look the the NEXT lower neighbor. // t = x[j]; x[j] = x[j-1]; x[j-1] = t; } } // // Sorting should be done now. Print the array again. // printf ( "\n" ); printf ( "Array after sorting:\n" ); printf ( "\n" ); for ( i = 0; i < 10; i++ ) { printf ( "x[%i] = %i\n", i, x[i] ); } return 0; }