Advertisement
Krefeld187

Untitled

Dec 13th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class suchen {
  4.  
  5. public static void main(String[] args)
  6. {
  7. //int
  8. int länge = 10;
  9. int key = 4;
  10. int feld[] = new int[länge];
  11. erzeugen(feld, 10);
  12. quicksort_rechts_Pivot(feld, 0, feld.length-1);
  13. ausgeben(feld);
  14. int erg = lineareSuche(feld, key);
  15. System.out.println("Zu suchen ist :" + key );
  16. System.out.println("lineare Suche :" + erg);
  17. int temp = runBinarySearchIteratively(feld, key, 0, feld.length-1);
  18. System.out.println("Binäre Suche :" + temp);
  19.  
  20. System.out.println();
  21. System.out.println("String");
  22. System.out.println();
  23.  
  24. //Strings
  25. String key2 = "abc";
  26. int length = 10;
  27. String arr [] = new String[length];
  28. erzeugen(length, arr);
  29. // arr[0] = "abc";
  30. ausgeben(arr);
  31. quicksort(arr, 0, arr.length-1);
  32. ausgeben(arr);
  33. int temp2 = linSuche2(arr, key2);
  34. int temp3 = binareSucheIte(arr, key2);
  35. System.out.println("Zu suchen ist :" + key2 );
  36. System.out.println("lineare Suche :" + temp2);
  37. System.out.println("Binäre Suche :" + temp3);
  38. }
  39.  
  40. static public void erzeugen(int[] feld, int max)
  41. {
  42. Random r = new Random();
  43. for(int i = 0; i < feld.length;++i)
  44. {
  45. feld[i] = r.nextInt(max)+1;
  46. }
  47. }
  48.  
  49. static public void ausgeben(int[] feld)
  50. {
  51. for(int i = 0; i < feld.length;++i)
  52. {
  53. System.out.print(feld[i] + " ");
  54. }
  55. System.out.println();
  56. }
  57.  
  58. static void quicksort_rechts_Pivot(int a[], int l, int r)
  59. {
  60. int v, i, j , t;
  61.  
  62. if (r > l)
  63. {
  64. v = a[r];
  65. i = l-1;
  66. j = r;
  67. for (;;)
  68. {
  69. while (a[++i] < v) ;
  70. while (j > 0 && a[--j] > v) ;
  71. if (i >= j)
  72. break;
  73. t = a[i];
  74. a[i] = a[j];
  75. a[j] = t;
  76. }
  77. t = a[i];
  78. a[i] = a[r];
  79. a[r] = t;
  80. quicksort_rechts_Pivot(a, l, i-1);
  81. quicksort_rechts_Pivot(a, i+1, r);
  82. }
  83. }
  84.  
  85. static int lineareSuche(int [] arr,int zahl)
  86. {
  87. for(int i = 0; i< arr.length; ++i)
  88. {
  89. if(arr[i] == zahl)
  90. return i;
  91. }
  92. return -1;
  93. }
  94.  
  95. static public int runBinarySearchIteratively(int[] sortedArray, int key, int links, int rechts)
  96. {
  97. while (links <= rechts)
  98. {
  99. int mid = (links + rechts) / 2;
  100. if (sortedArray[mid] < key)
  101. {
  102. // If key is greater, ignore left half
  103. links = mid + 1;
  104. } else if (sortedArray[mid] > key)
  105. {
  106. // If key is smaller, ignore right half
  107. rechts = mid - 1;
  108. } else if (sortedArray[mid] == key)
  109. {
  110. // Check if key is present at mid
  111. return mid;
  112. }
  113. }
  114. return -1;
  115. }
  116.  
  117. static int binarySearch(int arr[], int links, int rechts, int key)
  118. {
  119. if(links > rechts)
  120. return -1;
  121.  
  122. int mid = (links +rechts )/ 2;
  123. if(arr[mid] == key)
  124. return mid;
  125. else if(key < arr[mid])
  126. return binarySearch(arr,links, mid-1, key);
  127. else
  128. return binarySearch(arr,mid +1, rechts, key);
  129. }
  130.  
  131.  
  132. // String Suche
  133. static String allowedChars ="0123456789abcdefghijklmnopqrstuvwxyz";
  134.  
  135. private static String generateRandomString(String allowedChars, Random random, int laenge)
  136. {
  137. int max = allowedChars.length();
  138. StringBuffer buffer = new StringBuffer();
  139. for (int i=0; i<laenge; i++)
  140. {
  141. int value = random.nextInt(max);
  142. buffer.append(allowedChars.charAt(value));
  143. }
  144. return buffer.toString();
  145. }
  146.  
  147. static public void erzeugen(int stringlaenge,String[] arr)
  148. {
  149. Random random = new Random();
  150. for (int i=0; i<arr.length; i++)
  151. {
  152. arr[i] = generateRandomString(allowedChars, random, stringlaenge);
  153. }
  154. }
  155.  
  156. static public void ausgeben(String[] arr)
  157. {
  158. for(int i = 0; i < arr.length;++i)
  159. {
  160. System.out.print(arr[i] + " ");
  161. }
  162. System.out.println();
  163. }
  164.  
  165. static public int linSuche2(String[] feld, String a)
  166. {
  167. for (int i=0; i<feld.length; i++)
  168. {
  169. if (a.equals(feld[i]))
  170. {
  171. return i;
  172. }
  173. }
  174. return -1;
  175. }
  176.  
  177. static public int binareSucheIte(String[] feld, String gzahl)
  178. {
  179. int links = 0;
  180. int rechts = feld.length-1;
  181. int mitte;
  182. while(links<=rechts)
  183. {
  184. mitte = (links+rechts)/2;
  185.  
  186. if(feld[mitte].equals(gzahl))
  187. {
  188. return mitte;
  189. }
  190.  
  191. if(feld[mitte].compareTo(gzahl)>0)
  192. {
  193. rechts = mitte-1;
  194. }
  195. else
  196. {
  197. links = mitte+1;
  198. }
  199. }
  200. return -1;
  201. }
  202.  
  203. static int binarySearch(String arr[], int links, int rechts, String key)
  204. {
  205. if(links > rechts)
  206. return -1;
  207.  
  208. int mid = (links + rechts )/ 2;
  209. if(arr[mid].equals(key))
  210. return mid;
  211. else if(key.compareTo(arr[mid]) < 0)
  212. return binarySearch(arr,links, mid-1, key);
  213. else
  214. return binarySearch(arr,mid +1, rechts, key);
  215. }
  216.  
  217. static void quicksort(String a[], int l, int r)
  218. {
  219. // v: v ist das Pivot-Element
  220. // i: i läuft von links nach rechts (Laufindex)
  221. // j: j läuft von rechts nach links (Laufindex)
  222. String v;
  223. int i, j;
  224. // Solange die rechte Grenze größer ist als die linke
  225. if (r>l)
  226. {
  227. // die rechte Grenze wird als Pivot-Element gesetzt
  228. v = a[r];
  229. // i wird auf eins weniger als die linke Grenze gesetzt
  230. i=l-1;
  231. // j wird auf die rechte Grenze gesetzt
  232. j=r;
  233. // endlos Schleife, wartet auf ein break
  234. for(;;)
  235. {
  236. // sucht das nächste Element sodass a[i] > v
  237. while (a[++i].compareTo(v) < 0);
  238. // sucht das nächste Element sodass a[j] < v
  239. // j > 0 ist notwendig, weil sonst j negativ werden kann
  240. while (j > 0 && a[--j].compareTo(v) > 0);
  241. // Abfrage nach Überkreuzung
  242. if (i>=j) break;
  243. // Wenn keine Überkreuzung stattgefunden hat,
  244. // wird a[i] mit a[j] getauscht
  245. swap(a,i,j);
  246. }
  247. // das Pivot-Element wird an die richtige
  248. // Stelle gebracht
  249. swap(a,i,r);
  250. // rekursiver Aufruf für linkes und rechtes
  251. // Subarray, Element an Addresse i bleibt
  252. // unverändert
  253. quicksort (a,l, i-1);
  254. quicksort(a,i+1,r);
  255. }
  256. }
  257.  
  258. public static void swap(String[] arr, int a, int b)
  259. {
  260. String temp = arr[a];
  261. arr[a] = arr[b];
  262. arr[b] = temp;
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement