Advertisement
Guest User

Untitled

a guest
May 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. //import Prog1Tools.IOTools;
  2. public class BubbleSort
  3. {
  4.  
  5. public static int[] randomSequence;
  6.  
  7. public static int[] generateNewRandomArray (int x)
  8. {
  9.  
  10. randomSequence = new int[x];
  11.  
  12. for(int i = 0 ; i<randomSequence.length ; i++)
  13. {
  14. double DezimalRandom = Math.random();
  15.  
  16.  
  17. randomSequence [i] = (int)(DezimalRandom*10);
  18.  
  19. }
  20.  
  21. return randomSequence;
  22. }
  23.  
  24.  
  25.  
  26. public static void main (String[] args)
  27. {
  28.  
  29. //int customlength = IOTools.readInt("Was ist die gewünschte Array Länge?: ");
  30.  
  31. generateNewRandomArray (20);
  32.  
  33. printArrayToConsole(randomSequence);
  34.  
  35. sortArray(randomSequence);
  36.  
  37. //printArrayToConsole(randomSequence);
  38. }
  39.  
  40.  
  41. public static int[] printArrayToConsole(int[] x)
  42. {
  43.  
  44. for(int i = 0 ; i<x.length ; i++)
  45. {
  46. System.out.print(x[i]);
  47. }
  48.  
  49. System.out.println(" "); //Absatz
  50.  
  51. return x;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. public static int[] sortArray(int[] a)
  58. {
  59. int n = a.length-1 ;
  60. boolean vertauscht = false;
  61.  
  62. do
  63. {
  64.  
  65. for(int i = 0 ; i<n ; i++)
  66. {
  67. if(a[i]>a[i+1])
  68. {
  69. int temp = a[i];
  70. a[i] = a[i+1];
  71. a[i+1] = temp;
  72. vertauscht = true;
  73. }
  74. }
  75.  
  76. n = n -1;
  77. }
  78. while(vertauscht & n>=1);
  79.  
  80. for(int i = 0 ; i<a.length ; i++)
  81. {
  82. System.out.print(a[i]);
  83. }
  84.  
  85.  
  86. return a ;
  87.  
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement