Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. public class SearchAndSort
  2. {
  3. //precondition: values is a non-empty array of integers
  4. //postcondition: return the index of the target, or -1 if target is not in the array
  5. public static int linearSearch(int[] values, int target)
  6. {
  7. for(int x = 0; x < values.length; x++)
  8. {
  9. if (values[x] == target)
  10. return x;
  11. }
  12. return -1;
  13. }
  14.  
  15. //overloaded for String arrays
  16. public static int linearSearch(String[] values, String target)
  17. {
  18. for(int x = 0; x < values.length; x++)
  19. {
  20. if(values[x].equals(target))
  21. return x;
  22. }
  23. return -1;
  24. }
  25.  
  26. //precondition: values is already sorted
  27. //postcondition: return the index of the target, or -1 if target is not in the array
  28. public static int binarySearch(int[] values, int target)
  29. {
  30. int f = 0;
  31. int l = values.length -1;
  32. while(f<=l)
  33. {
  34. int m = (f+l)/2;
  35. if(values[m] == target)
  36.  
  37. return m;
  38. else if(values[m] > target)
  39. l = m-1;
  40. else if(values[m] < target)
  41. f = m+1;
  42.  
  43.  
  44.  
  45. }
  46. return -1;
  47. }
  48.  
  49. //String version
  50. public static int binarySearch(String[] values, String target)
  51. {
  52. int f = 0;
  53. int l = values.length -1;
  54.  
  55. while(f<=l)
  56. {
  57. int m = (f+l)/2;
  58. if(values[m].compareTo(target) == 0)
  59. return m;
  60. else if(values[m].compareTo(target) > 0)
  61. l = m-1;
  62. else if(values[m].compareTo(target) < 0)
  63. f = m+1;
  64. }
  65. return -1;
  66. }
  67.  
  68. //precondition: values is a non-empty array of integers
  69. //postcondition: returns a new array that is the sorted version of the input parameter
  70. public static int[] bubbleSort(int[] values)
  71. {
  72. for(int spot = 0; spot < values.length; spot++)
  73. {
  74. for(int x = 0; x < values.length-spot-1; x++)
  75. {
  76. int temp = 0;
  77. if(values[x] > values[x+1])
  78. {
  79. temp = values[x];
  80. values[x] = values[x+1];
  81. values[x+1] = temp;
  82. }
  83. }
  84. }
  85. return values;
  86. }
  87.  
  88. //String version
  89. public static String[] bubbleSort(String[] values)
  90. {
  91. for(int spot = 0; spot < values.length; spot++)
  92. {
  93. for(int x = 0; x < values.length-spot-1; x++)
  94. {
  95. String temp = "";
  96. if(values[x].compareTo(values[x+1]) > 0)
  97. {
  98. temp = values[x];
  99. values[x] = values[x+1];
  100. values[x+1] = temp;
  101. }
  102. }
  103. }
  104. return values;
  105. }
  106.  
  107. //precondition: values is a non-empty array of integers
  108. //postcondition: returns a new array that is the sorted version of the input parameter
  109. public static int[] selectionSort(int[] values)
  110. {
  111. for(int i = 0; i< values.length -1; i++)
  112. {
  113. int spot = i;
  114. for(int x = i+ 1; x< values.length; x++)
  115. {
  116. if(values[x] < values[spot])
  117. spot = x;
  118. }
  119.  
  120. int temp = values[spot];
  121. values[spot] = values[i];
  122. values[i] = temp;
  123. }
  124. return values;
  125. }
  126.  
  127. //String version
  128. public static String[] selectionSort(String[] values)
  129. {
  130. for(int i = 0; i< values.length -1; i++)
  131. {
  132. int spot = i;
  133. for(int x = i+ 1; x< values.length; x++)
  134. {
  135. if(values[x].compareTo(values[spot])<0)
  136. spot = x;
  137. }
  138.  
  139. String temp = values[spot];
  140. values[spot] = values[i];
  141. values[i] = temp;
  142. }
  143. return values;
  144. }
  145.  
  146. //precondition: values is a non-empty array of integers
  147. //postcondition: returns a new array that is the sorted version of the input parameter
  148. public static int[] insertionSort(int[] values)
  149. {
  150. for (int i = 1; i < values.length; i++)
  151. {
  152. int key = values[i];
  153. int j = i-1;
  154.  
  155. while (j >= 0 && values[j] > key)
  156. {
  157. values[j+1] = values[j];
  158. j = j-1;
  159. }
  160. values[j+1] = key;
  161. }
  162. return values;
  163. }
  164.  
  165. //String version
  166. public static String[] insertionSort(String[] values)
  167. {
  168. for (int i = 1; i < values.length; i++)
  169. {
  170. String key = values[i];
  171. int j = i-1;
  172.  
  173. while (j >= 0 && values[j].compareTo(key) > 0)
  174. {
  175. values[j+1] = values[j];
  176. j = j-1;
  177. }
  178. values[j+1] = key;
  179. }
  180. return values;
  181. }
  182.  
  183. //Janssensort method
  184. public static int[] JanssenSort(int[] values)
  185. {
  186. //loop through the array to mark the starting point
  187. for(int spot = 0; spot < values.length; spot++)
  188. {
  189. //loop through each value of the array
  190. for(int x = 0; x < values.length-spot-1; x++)
  191. {
  192. //swap the values if one is less than the other
  193. int temp = 0;
  194. if(values[x] > values[x+1])
  195. {
  196. temp = values[x];
  197. values[x] = values[x+1];
  198. values[x+1] = temp;
  199. }
  200. }
  201. }
  202. //return the sorted array
  203. return values;
  204. }
  205.  
  206. public static void main(String[] args)
  207. {
  208. int[] array = new int[]{12, 3, 25, -4, -13, 7, 5, 32, 19, -3, 0};
  209. JanssenSort(array);
  210. for(int x = 0; x < array.length; x++)
  211. {
  212. System.out.println(array[x]);
  213. }
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement