Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. Sorts.java
  2. public class Sorts
  3. {
  4. public static void main (String[] args)
  5. {
  6. int[] grades = {89, 94, 69, 80, 97, 85, 73, 91, 77, 85, 93};
  7.  
  8. DecendingSorts.selectionSort (grades);
  9.  
  10. for (int grade : grades)
  11. System.out.print (grade + " ");
  12. }
  13. }
  14.  
  15. and DecendingSorts.java
  16. public class DecendingSorts
  17. {
  18. //-----------------------------------------------------------------
  19. // Sorts the specified array of integers using the selection
  20. // sort algorithm.
  21. //-----------------------------------------------------------------
  22. public static void selectionSort (int[] numbers)
  23. {
  24. int min, temp;
  25.  
  26. for (int index = 0; index < numbers.length-1; index++)
  27. {
  28. min = index;
  29. for (int scan = index+1; scan < numbers.length; scan++)
  30. if (numbers[scan] < numbers[min])
  31. min = scan;
  32.  
  33. // Swap the values
  34. temp = numbers[min];
  35. numbers[min] = numbers[index];
  36. numbers[index] = temp;
  37. }
  38. }
  39.  
  40. //-----------------------------------------------------------------
  41. // Sorts the specified array of integers using the insertion
  42. // sort algorithm.
  43. //-----------------------------------------------------------------
  44. public static void insertionSort (int[] numbers)
  45. {
  46. for (int index = 1; index < numbers.length; index++)
  47. {
  48. int key = numbers[index];
  49. int position = index;
  50.  
  51. // shift larger values to the right
  52. while (position > 0 && numbers[position-1] > key)
  53. {
  54. numbers[position] = numbers[position-1];
  55. position--;
  56. }
  57.  
  58.  
  59. numbers[position] = key;
  60. }
  61. }
  62.  
  63. //-----------------------------------------------------------------
  64. // Sorts the specified array of objects using the insertion
  65. // sort algorithm.
  66. //-----------------------------------------------------------------
  67. public static void insertionSort (Comparable[] objects)
  68. {
  69. for (int index = 1; index < objects.length; index++)
  70. {
  71. Comparable key = objects[index];
  72. int position = index;
  73.  
  74. // shift larger values to the right
  75. while (position > 0 && objects[position-1].compareTo(key) > 0)
  76. {
  77. objects[position] = objects[position-1];
  78. position--;
  79. }
  80.  
  81. objects[position] = key;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement