Dadhawk

Untitled

Nov 26th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. public static void selectionSort(int[] array)
  2.     {
  3.         //two useful variable
  4.         int minValue; //lowest value in the array
  5.         int minValueIndex; //index where minValue is found
  6.         //nested for loop, outer loop does the passes
  7.         for(int leftElementIndex = 0; leftElementIndex < array.length; leftElementIndex++)
  8.         {
  9.             //for each iteration of the outer loop do these two assignments
  10.             minValue = array[leftElementIndex];
  11.             minValueIndex = leftElementIndex;
  12.             //inner loop just finds the lowest value and its index
  13.             for(int innerIndex = leftElementIndex + 1; innerIndex < array.length; innerIndex++)
  14.             {
  15.                 if(array[innerIndex] < minValue)
  16.                 {
  17.                     //we've got a new minValue
  18.                     minValue = array[innerIndex];
  19.                     minValueIndex = innerIndex;
  20.                 }//end if
  21.             }//end inner for
  22.             MyToolbox.swapArrayElements(array, leftElementIndex, minValueIndex);
  23.         }//end outer for
  24.     }//end method
Advertisement
Add Comment
Please, Sign In to add comment