Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. // Code which tests the selectionSort() method. We'll create arrays of various
  9. // sizes, some that are already sorted and some that are not, and call selectionSort
  10. // on them. We have a separate method which checks the results.
  11.  
  12. // Array with just a single element (already sorted).
  13. double[] array1 = { 12.4 };
  14. double[] result1 = selectionSort(array1);
  15. assert isArraySorted(result1);
  16.  
  17. // Array with 3 elements, unsorted.
  18. double[] array2 = { 12.4, 7.5, -1.1 };
  19. double[] result2 = selectionSort(array2);
  20. assert isArraySorted(result2);
  21.  
  22. // Array with 5 elements, sorted.
  23. double[] array3 = { -5.4, -1.1, 6.2, 9.8, 73.0 };
  24. double[] result3 = selectionSort(array3);
  25. assert isArraySorted(result3);
  26.  
  27. // Array with 8 elements, unsorted.
  28. double[] array4 = { 99.9, -41.3, 73.0, -17.4, -8.1, 23.7, 106.2, 49.4 };
  29. double[] result4 = selectionSort(array4);
  30. assert isArraySorted(result4);
  31.  
  32. // Array with 20 randomly-generated elements between 0 - 1000.
  33. double[] array5 = new double[20];
  34. for (int i = 0; i < array5.length; i++) {
  35. array5[i] = Math.random() * 1000.0;
  36. }
  37. double[] result5 = selectionSort(array5);
  38. assert isArraySorted(result5);
  39. }
  40.  
  41. /**
  42. * An implementation of the selection sort algorithm to sort an array of floating-point numbers.
  43. * Selection sort works by "selecting" the smallest element in the array and placing it first,
  44. * then the next-smallest element and placing it second, etc., until all elements are selected
  45. * and placed properly.
  46. * @param array Array to be sorted.
  47. * @return A sorted version of the passed-in array.
  48. */
  49. public static double[] selectionSort(double[] array) {
  50. for(int a = 0; a < array.length - 1; a++) {
  51. int min_idx = a;
  52. for(int b = a + 1; b < array.length; b++) {
  53. if(array[b] < array[min_idx]) {
  54. min_idx = b;
  55. }
  56. }
  57. double temp_arr = array[min_idx];
  58. array[min_idx] = array[a];
  59. array[a] = temp_arr;
  60. }
  61. return array;
  62. }
  63.  
  64. /**
  65. * Check to see if an array of doubles are sorted.
  66. *
  67. * Precondition: there are no duplicate elements.
  68. * @param array The array of doubles to check.
  69. * @return true if the array is sorted, false otherwise. Returns true for an array of length 0
  70. */
  71. public static boolean isArraySorted(double[] array) {
  72. // For an array of length 0, there's nothing to check, so we
  73. // just return true.
  74. if (array.length < 1) {
  75. return true;
  76. }
  77.  
  78. // We compare every element in the array to the previous element; if it's less than
  79. // or equal to the previous element, the array isn't sorted, so we return false. (The
  80. // reason we can include "or equal to" in our check is because we have the precondition
  81. // in all of our tests that none of the original arrays have duplicate items.
  82. for (int i = 1; i < array.length; i++) {
  83. if (array[i] <= array[i-1]) {
  84. return false;
  85. }
  86. }
  87.  
  88. // If we make it to the end of the array without returning false, it means all of the
  89. // elements are in increasing order, so we can return true that the array is sorted.
  90. return true;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement