Advertisement
FenrirsCode

Sorts

May 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. /*
  2. * Sorts.java
  3. */
  4. package SortingFiles;
  5.  
  6. /**
  7. *
  8. * @author Mathew Harmon
  9. */
  10. public class Sorts
  11.  
  12. {
  13. /**
  14. *
  15. * @param array
  16. * Description: which merely calls quickSort(array, 0, array.length - 1)
  17. */
  18.  
  19.  
  20.  
  21. public static void quickSort(Comparable[] array)
  22. throws java.lang.ArrayIndexOutOfBoundsException{
  23. quickSort(array, 0, array.length - 1);
  24.  
  25. }
  26. /**
  27. *
  28. * @param array
  29. * @param from
  30. * @param to
  31. * Description: if indices not in array which implements the Optimized
  32. quick sort algorithm as presented in class, utilizing the following
  33. methods: insertionSort,partition,swap, and sortFirstMiddleLast.
  34. * @see insertionSort
  35. * @see partition
  36. * @see swap
  37. * @see sortFirstMiddleLast
  38. */
  39. public static void quickSort(Comparable[] array, int from, int to)
  40. throws java.lang.ArrayIndexOutOfBoundsException {
  41.  
  42. if( array.length <= THREE) {
  43. insertionSort(array, from, to);
  44. }
  45. else{
  46. int mid = partition(array, from, to);
  47. quickSort(array, from, mid -1);
  48. quickSort(array, mid +1, to);
  49. }
  50. }
  51. /**
  52. *
  53. * @param array
  54. * @param from
  55. * @param to
  56. */
  57. public static void insertionSort(Comparable[] array, int from, int to)
  58. throws java.lang.ArrayIndexOutOfBoundsException
  59. //i think this is right?....
  60. {
  61. Comparable temp;
  62. for(int i = from; from < array.length; i++){
  63. temp = array[i];
  64. int j = to;
  65. for(j = i; j > 0; j--)
  66. if(temp.compareTo(array[j - 1]) < 0)
  67. array[j] = array[j - 1];
  68. else
  69. break;
  70. array[j] = temp;
  71. }
  72.  
  73.  
  74.  
  75.  
  76. }
  77. /**
  78. *
  79. * @param array
  80. * @param from
  81. * @param to
  82. * Description: sets a midpoint, calls sortFirstMiddleLast,
  83. moves data around the pivot value, and returns the pivot index
  84. * @return
  85. */
  86. private static int partition(Comparable[] array, int from, int to)
  87. throws java.lang.ArrayIndexOutOfBoundsException {
  88. //not the right return just putting that for now
  89. //this is the main bulk of the code!
  90. sortFirstMiddleLast(array,from,to,mid);
  91. return 0;
  92. }
  93. /**
  94. *
  95. * @param array
  96. * @param from
  97. * @param to
  98. */
  99. private static void swap(Comparable[] array, int from, int to)
  100. throws java.lang.ArrayIndexOutOfBoundsException{
  101. Comparable swapped = array[from];
  102. array[from] = array[to];
  103. array[to] = swapped;
  104.  
  105.  
  106. }
  107. /**
  108. *
  109. * @param array
  110. * @param from
  111. * @param mid
  112. * @param to
  113. */
  114. private static void sortFirstMiddleLast(Comparable[] array, int from,
  115. int mid, int to)throws java.lang.ArrayIndexOutOfBoundsException{
  116. int a = from;
  117. int b = mid;
  118. int c = to;
  119.  
  120. swap(array,from,to);
  121. if(a > b){
  122. swap(array,a,b);
  123. }
  124. if(b > c){
  125. swap(array,b,c);
  126. }
  127. if(a < b){
  128. swap(array,a,b);
  129. }
  130.  
  131. }
  132.  
  133. static final int THREE = 3;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement