Guest User

Untitled

a guest
Jul 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Suchen1 {
  4.  
  5. public static void spalten(int[] n, int zahl) {
  6. int laenge = n.length;
  7. if (laenge % 2 == 0) {
  8. int[] n1 = new int[laenge / 2];
  9. int[] n2 = new int[laenge / 2];
  10. for (int i = 0; i < laenge / 2; i++) {
  11. n1[i] = n[i];
  12. n2[i] = n[laenge / 2 + i];
  13. }
  14.  
  15. if (zahl <= n1[laenge / 2 - 1]) {
  16. spalten(n1, zahl);
  17. } else if (zahl >= n2[0]) {
  18. spalten(n2, zahl);
  19. } else {
  20. System.out.println("Zahl nicht enthalten");
  21. }
  22.  
  23. } else {
  24.  
  25. System.out.println("Die Zahl ist enthalten, Zahl: " + zahl);
  26.  
  27. }
  28.  
  29. }
  30.  
  31. /*
  32. * Braucht man nicht mehr
  33. *
  34. * public static void durchzaehlen(int[] n, int zahl) { boolean treffer =
  35. * false; for (int i = n.length - 1; i >= 0 && treffer == false; i--) { if
  36. * (n[i] == zahl) { treffer = true;
  37. * System.out.println("Treffer Von Oben, die Zahl war " + zahl); for (int j
  38. * = 0; j < n.length; j++) { System.out.println(n[j]); }
  39. *
  40. * } } if (treffer == false) {
  41. * System.out.println("Kein Treffer, die Zahl war " + zahl); } }
  42. */
  43. public static void main(String[] args) {
  44.  
  45. Random random = new Random();
  46. int groesse = 4096;
  47. int zahlenbereich = 10000;
  48.  
  49. int[] n = new int[groesse];
  50. for (int i = 0; i < groesse; i++) {
  51. n[i] = random.nextInt(zahlenbereich);
  52. }
  53. java.util.Arrays.sort(n);
  54. spalten(n, random.nextInt(zahlenbereich));
  55. }
  56.  
  57. }
Add Comment
Please, Sign In to add comment