thorax232

Java - Selection Sort (faster)

Nov 8th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. public static void selectionSort2(int[] x) {
  2.     for (int i=0; i<x.length-1; i++) {
  3.         int minIndex = i;      // Index of smallest remaining value.
  4.         for (int j=i+1; j<x.length; j++) {
  5.             if (x[minIndex] > x[j]) {
  6.                 minIndex = j;  // Remember index of new minimum
  7.             }
  8.         }
  9.         if (minIndex != i) {
  10.             //...  Exchange current element with smallest remaining.
  11.             int temp = x[i];
  12.             x[i] = x[minIndex];
  13.             x[minIndex] = temp;
  14.         }
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment