Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void selectionSort(int[] array)
- {
- //two useful variable
- int minValue; //lowest value in the array
- int minValueIndex; //index where minValue is found
- //nested for loop, outer loop does the passes
- for(int leftElementIndex = 0; leftElementIndex < array.length; leftElementIndex++)
- {
- //for each iteration of the outer loop do these two assignments
- minValue = array[leftElementIndex];
- minValueIndex = leftElementIndex;
- //inner loop just finds the lowest value and its index
- for(int innerIndex = leftElementIndex + 1; innerIndex < array.length; innerIndex++)
- {
- if(array[innerIndex] < minValue)
- {
- //we've got a new minValue
- minValue = array[innerIndex];
- minValueIndex = innerIndex;
- }//end if
- }//end inner for
- MyToolbox.swapArrayElements(array, leftElementIndex, minValueIndex);
- }//end outer for
- }//end method
Advertisement
Add Comment
Please, Sign In to add comment