Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class QuickSortSchueler {
- public static void main(String[] args) {
- int[] werte = zufaelligeZahlen();
- quicksort(werte, 0, werte.length - 1);
- ausgeben(werte);
- } // end of main
- public static int[] zufaelligeZahlen() {
- int[] zahlen = new int[250];
- for (int i = 1; i <= 250; i++) {
- int pos = 0;
- while (zahlen[pos] != 0)
- pos = (int) Math.round(Math.random() * 249);
- zahlen[pos] = i;
- }
- return zahlen;
- }
- public static void ausgeben(int[] zahlen) {
- String res = "";
- for (int i = 0; i < zahlen.length; i++) {
- res = res + "; " + zahlen[i];
- } // end of for
- System.out.println(res);
- }
- /** Ab hier die eigentliche Sortierung */
- public static void quicksort(int[] zahlen, int lo, int hi) {
- if (hi - lo > 0) {
- int pivot = zahlen[(hi + lo) / 2];
- int i, j;
- i = lo;
- j = hi;
- while (i < j) {
- while (zahlen[i] < pivot) {
- i++;
- } // end of while
- while (zahlen[j] > pivot) {
- j--;
- } // end of while
- int temp = zahlen[i];
- zahlen[i] = zahlen[j];
- zahlen[j] = temp;
- } // end of while
- quicksort(zahlen, lo, i - 1);
- quicksort(zahlen, i + 1, hi);
- } // end of if
- }
- } // end of class QuickSort
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement