Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. public class SelectionSort {
  2.   /** The method for sorting the numbers */
  3.   public static void selectionSort(double[] list) {
  4.     for (int i = 0; i < list.length - 1; i++) {
  5.       // Find the minimum in the list[i..list.length-1]
  6.       double currentMin = list[i];
  7.       int currentMinIndex = i;
  8.  
  9.       for (int j = i + 1; j < list.length; j++) {
  10.         if (currentMin > list[j]) {
  11.           currentMin = list[j];
  12.           currentMinIndex = j;
  13.         }
  14.       }
  15.  
  16.       // Swap list[i] with list[currentMinIndex] if necessary;
  17.       if (currentMinIndex != i) {
  18.         list[currentMinIndex] = list[i];
  19.         list[i] = currentMin;
  20.       }
  21.     }
  22.   }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement