All pastes #916523 Raw Edit

Something

public text v1 · immutable
#916523 ·published 2008-02-24 18:56 UTC
rendered paste body


   |   void SelectionSort(InputArray A, int m, int n)
   |   {
   |      int   MaxPosition;    /* MaxPosition is the index of A's biggest item  */
   |      int   temp;                     /* temp is used to exchange items in A */
 5 |
   |   
   |      if (m < n) {               /* if there is more than one number to sort */
   |      
   |         /* Let MaxPosition be the index of the largest number in A[m:n] */
10 |            MaxPosition = FindMax(A,m,n);
   |         
   |         /* Exchange A[m] <--> A[MaxPosition] */
   |            temp = A[m];
   |            A[m] = A[MaxPosition];
15 |            A[MaxPosition] = temp;
   |         
   |         /* SelectionSort the subarray A[m+1:n] */
   |            SelectionSort(A, m+1, n);
   |         
20 |      }
   |   
   |   }

Program 5.19 Selection Sorting

/*********************************************************************************/


   |   int FindMax(InputArray A, int m, int n)   /* assume m<n */
   |   {
   |      int i = m;      /* i is an index that visits all positions from m to n */
   |      int j = m;     /* j is an index that saves the position of the largest */
 5 |               
   |                        
   |      do {
   |         i++;                      /* advance i to point to next number A[i] */
   |         if (A[i] > A[j]) {          /* if A[i] > largest previous A[j] then */
10 |            j = i;              /* save the position, i, of the largest in j */
   |         }
   |      } while (i != n);           /* stop when all i in m:n have been tested */
   |   
   |      return j;                       /* return j == position of the largest */
   |   }                                                /* number A[j] in A[m:n] */

Program 5.20 Finding the Position of the Largest Number