UNB/ CS/ David Bremner/ tags/ snippet/ sorting

This feed contains pages in the "sorting" category.

public class SelectionSort
{  /**
      Finds the smallest element in
      an array range.
      @param a the array to search
      @param from the first position
        in a to compare
      @return the position of the
      smallest element in the range
      a[from]...a[a.length - 1]
   */
    public static int
        minimumPosition(int[] a, int from)  {
        int minPos = from;
        for (int i = from + 1; i < a.length; i++){
            if (a[i] < a[minPos])
                minPos = i;
        }

        return minPos;
    }
    /**
       Sorts an array.
       @param a the array to sort
    */
    public static void sort(int[] a)
    {
        for (int n = 0; n < a.length - 1; n++) {
            int minPos = minimumPosition(a, n);
            if (minPos != n){
                // swap
                int temp=a[n];
                a[n]=a[minPos];
                a[minPos]=temp;
            }
        }
    }
}
//

Posted Wed 09 Mar 2011 07:43:08 PM AST Tags: /tags/snippet/sorting
License: by-nc-sa-2.5