Download code

Jump to: navigation, search

Back to Selection_sort_(Java)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

SelectionSortExample.java

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Selection_sort_(Java)?oldid=11987
 14 */
 15 
 16 import java.util.Comparator;
 17 import java.util.Random;
 18 
 19 class SelectionSortExample
 20 {
 21     public static void selectionSortIntArray(int[] a)
 22     {
 23         for (int sortedSize=0; sortedSize < a.length; sortedSize++)
 24         {
 25             int minElementIndex = sortedSize;
 26 	    int minElementValue = a[sortedSize];
 27 	    for (int i = sortedSize + 1; i < a.length; i++) 
 28 	    {
 29 	        if (a[i] < minElementValue)
 30 	        {
 31 	            minElementIndex = i;
 32 	            minElementValue = a[i];
 33 	        }
 34 	    }
 35             a[minElementIndex] = a[sortedSize];
 36 	    a[sortedSize] = minElementValue;
 37         }
 38     }
 39 
 40 
 41     public static <T extends Comparable<? super T>> void selectionSortGeneric(T[] a)
 42     {
 43         for (int sortedSize=0; sortedSize < a.length; sortedSize++)
 44         {
 45             int minElementIndex = sortedSize;
 46             T minElementValue = a[sortedSize];
 47             for (int i = sortedSize + 1; i < a.length; i++) 
 48             {
 49                 if (a[i].compareTo(minElementValue) < 0)
 50                 {
 51                     minElementIndex = i;
 52                     minElementValue = a[i];
 53                 }
 54             }
 55             a[minElementIndex] = a[sortedSize];
 56             a[sortedSize] = minElementValue;
 57         }
 58     }
 59 
 60     public static <T> void selectionSortGeneric(T[] a, Comparator<? super T> comparer)
 61     {
 62         for (int sortedSize = 0; sortedSize < a.length; sortedSize++)
 63         {
 64             int minElementIndex = sortedSize;
 65             T minElementValue = a[sortedSize];
 66             for (int i = sortedSize + 1; i < a.length; i++) 
 67             {
 68                 if (comparer.compare(a[i], minElementValue) < 0)
 69                 {
 70                     minElementIndex = i;
 71                     minElementValue = a[i];
 72                 }
 73             }
 74             a[minElementIndex] = a[sortedSize];
 75             a[sortedSize] = minElementValue;
 76         }
 77     }
 78 
 79     public static void main(String[] args)
 80     {
 81         int length = Integer.parseInt(args[0]);
 82         int[] a = new int[length];
 83         Random r = new Random();
 84 
 85         for (int i = 0; i < length; i++)
 86         {
 87             a[i] = r.nextInt();
 88         }
 89 
 90         long start = System.currentTimeMillis();
 91         selectionSortIntArray(a);
 92         System.out.println("Sort time in ms: " + (System.currentTimeMillis() - start));
 93 
 94         for (int i = 1; i < length; i++)
 95         {
 96             if (a[i] < a[i - 1])
 97             {
 98                 System.out.println("ERROR");
 99                 return;
100             }
101         }
102     }
103 }


hijacker
hijacker
hijacker
hijacker