Advertisement
Guest User

JOoooooooooooooooooooooooooooooooooo

a guest
Sep 16th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. public class QuickSortSchueler {
  2.  
  3.     public static void main(String[] args) {
  4.         int[] werte = zufaelligeZahlen();
  5.         quicksort(werte, 0, werte.length - 1);
  6.         ausgeben(werte);
  7.     } // end of main
  8.  
  9.     public static int[] zufaelligeZahlen() {
  10.         int[] zahlen = new int[250];
  11.         for (int i = 1; i <= 250; i++) {
  12.             int pos = 0;
  13.             while (zahlen[pos] != 0)
  14.                 pos = (int) Math.round(Math.random() * 249);
  15.             zahlen[pos] = i;
  16.         }
  17.         return zahlen;
  18.     }
  19.  
  20.     public static void ausgeben(int[] zahlen) {
  21.         String res = "";
  22.         for (int i = 0; i < zahlen.length; i++) {
  23.             res = res + "; " + zahlen[i];
  24.         } // end of for
  25.         System.out.println(res);
  26.     }
  27.  
  28.     /** Ab hier die eigentliche Sortierung */
  29.     public static void quicksort(int[] zahlen, int lo, int hi) {
  30.         if (hi - lo > 0) {
  31.             int pivot = zahlen[(hi + lo) / 2];
  32.             int i, j;
  33.             i = lo;
  34.             j = hi;
  35.             while (i < j) {
  36.                
  37.                 while (zahlen[i] < pivot) {
  38.                     i++;
  39.                 } // end of while
  40.  
  41.                 while (zahlen[j] > pivot) {
  42.                     j--;
  43.                 } // end of while
  44.                 int temp = zahlen[i];
  45.                 zahlen[i] = zahlen[j];
  46.                 zahlen[j] = temp;
  47.             } // end of while
  48.             quicksort(zahlen, lo, i - 1);
  49.             quicksort(zahlen, i + 1, hi);
  50.         } // end of if
  51.     }
  52. } // end of class QuickSort
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement