This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

SelectionSort

By: a guest on Jan 23rd, 2013  |  syntax: None  |  size: 0.63 KB  |  views: 46  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. public class SelectionSort {
  3.        
  4.         public SelectionSort(int[] a)
  5.     {
  6.        
  7.         //this.a = a;
  8.         //shallow copy
  9.         //OR THIS?
  10.         //deep copy
  11.         this.a = new int[a.length];
  12.         for(int i = 0; i < a.length; i++)
  13.                 this.a[i] = a[i];
  14.  
  15.     }
  16.    
  17.     public int sort()
  18.     {
  19.         for (int i = 0; i < a.length; i++)
  20.         {
  21.                 int min = ArrayUtil.minimumPosition(i, a);
  22.                 ArrayUtil.swap(min, i, a);
  23.                 swaps++;                       
  24.         }
  25.         return swaps;
  26.     }
  27.    
  28.  
  29.     public int[] getArray()
  30.     {
  31.         return a;
  32.     }
  33.  
  34.     public int getSwaps()
  35.     {
  36.         return swaps;
  37.     }
  38.    
  39.     private int swaps;
  40.     private int[] a;
  41. }
clone this paste RAW Paste Data