Advertisement
Dido09

Selection Sort

Mar 28th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1.  
  2. import java.util.Arrays;
  3.  
  4. public class SelectionSortExample {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. int[] array = {10, 8, 99, 7, 1, 5, 88, 9};
  9.  
  10. selection_sort(array);
  11.  
  12. System.out.println(Arrays.toString(array));
  13.  
  14. }
  15.  
  16. private static void selection_sort(int[] input) {
  17.  
  18. int inputLength = input.length;
  19.  
  20. for (int i = 0; i < inputLength - 1; i++) {
  21.  
  22. int min = i;
  23.  
  24. // find the first, second, third, fourth... smallest value
  25. for (int j = i + 1; j < inputLength; j++) {
  26. if (input[j] < input[min]) {
  27. min = j;
  28. }
  29. }
  30.  
  31. // swaps the smallest value with the position 'i'
  32. int temp = input[i];
  33. input[i] = input[min];
  34. input[min] = temp;
  35.  
  36.  
  37. }
  38.  
  39. }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement