Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public static int[] doInsertionSort(int[] array) {
  2.  
  3. int zwischenspeicher; // Zwischenspeicher erstellen (FÜR DEN TAUSCH)
  4.  
  5. for (int i = 1; i < array.length; i++) { // Durchlauf fängt mit der Hausnummer 2 an
  6.  
  7. for (int SortZahl = i; SortZahl > 0; SortZahl--) { // Immer durchsuchen, wo die unsortierte Zahl ist
  8.  
  9. if (array[SortZahl] < array[SortZahl - 1]) { // Tausch und die Zahlen in die richtigen Stellen...
  10. zwischenspeicher = array[SortZahl];
  11. array[SortZahl] = array[SortZahl - 1];
  12. array[SortZahl - 1] = zwischenspeicher;
  13. }
  14. }
  15. }
  16. return array;
  17. }
  18.  
  19. public static int[] doBubbleSort(int[] array) {
  20.  
  21. int zwischenspeicher;
  22.  
  23. for (int i = 0; i < array.length; i++) { // Bestimme die Anzahl der Tauschfensterdurchläufe
  24.  
  25. for (int linkesFenster = 0; linkesFenster < array.length - 1; linkesFenster++) { // Schiebt Tauschfenster von links nach rechts
  26.  
  27. int stelleRechts = linkesFenster + 1;
  28.  
  29. if (array[linkesFenster] > array[stelleRechts]) {
  30. zwischenspeicher = array[linkesFenster];
  31. array[linkesFenster] = array[stelleRechts];
  32. array[stelleRechts] = zwischenspeicher;
  33. }
  34.  
  35. }
  36. }
  37.  
  38. return array;
  39. }
  40.  
  41. public static int[] doOptimizedBubbleSort(int[] array) {
  42.  
  43. int zwischenspeicher;
  44.  
  45. int n = array.length;
  46.  
  47. do {
  48. int newn = 1;
  49.  
  50. for (int i = 0; i < n-1; i++) {
  51. if (array[i] > array[i+1]) {
  52.  
  53. zwischenspeicher = array[i];
  54. array[i] = array[i+1]; //SWAP
  55. array[i+1] = zwischenspeicher;
  56.  
  57. newn = i+1;
  58. }
  59. }
  60.  
  61. n = newn;
  62. } while (n > 1);
  63.  
  64. return array;
  65. }
  66.  
  67. public static int[] doSelectionSort(int[] array) {
  68. int gefundenesMinimum;
  69. int minimumPosition;
  70. int zwischenspeicher;
  71.  
  72. for (int stelleZuBesetzen = 0; stelleZuBesetzen < array.length; stelleZuBesetzen++) {
  73. gefundenesMinimum = array[stelleZuBesetzen];
  74. minimumPosition = stelleZuBesetzen;
  75.  
  76. // durchsuche unsortierten Bereich
  77. for (int i = stelleZuBesetzen; i < array.length; i++) {
  78. if (gefundenesMinimum > array[i]) {
  79. gefundenesMinimum = array[i];
  80. minimumPosition = i;
  81. }
  82. }
  83.  
  84. // Tausch
  85. zwischenspeicher = array[stelleZuBesetzen];
  86. array[stelleZuBesetzen] = array[minimumPosition];
  87. array[minimumPosition] = zwischenspeicher;
  88. }
  89. return array;
  90. }
  91.  
  92. public static void doBucketSort(int[] a, int b) {
  93. int[] buckets = new int[b];
  94.  
  95. for (int i = 0; i < a.length; i++) {
  96. buckets[a[i]]++;
  97. }
  98.  
  99. int c = 0;
  100.  
  101. for (int i = 0; i < b; i++) {
  102. while (buckets[i] > 0) {
  103. a[c] = i;
  104. c++;
  105. buckets[i]--;
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement