Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class SelectionSort
- {
- public static void main (String args []){
- System.out.println("\f");
- selectionSort();
- }
- public static void selectionSort() {
- int [] nums = {22,11,9,6,3,2,1};
- for (int i = 0; i < nums.length-1; i++) {
- int minIndex = i; // Assumed index of smallest remaining value.
- for (int j = i+1; j < nums.length; j++) {
- if (nums[j] < nums[minIndex] ) {
- minIndex = j; // Remember index of new minimum
- }
- }
- if (minIndex != i) {
- //Exchange current element with smallest remaining.
- //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
- int temp = nums[i];
- nums[i] = nums[minIndex];
- nums[minIndex] = temp;
- }
- System.out.println(Arrays.toString(nums));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement