Quicksort sortiert von groß nach klein

Hanschyo

Mitglied
Weiß jemand wo der Fehler liegt? Übringens als ich eine seperate Tauschfunktion gemacht habe um ein paar Zeilen wegzulassen hab ich StackOverFlow errors bekommen.
Java:
import java.util.Arrays;
public class Quicksort {
  public static void Quicksort(int[] x, int von, int bis) {
    int pivot = x[bis];
    int cursorL = von;
    int cursorR = bis;
    while (pivot<x[cursorL] && cursorL <= bis) {
      cursorL++;
    } // end of while
    while (pivot>x[cursorR] && cursorR >= 0) {
      cursorR--;
    } // end of while
    if (pivot==x[cursorL]) {
      if (cursorL!=0) {
        Quicksort(x, von, bis-1);
      } // end of if   
    }
    else {
      if (cursorL > cursorR) {
        int temp = x[cursorL];
        x[cursorL] = pivot;
        x[bis] = temp;
        if (cursorL!=0) {
          Quicksort(x, von, cursorL-1);
        } // end of if
        Quicksort(x , cursorL+1, bis);
      } else {
        if (cursorR > cursorL) {
          int temp = x[cursorL];
          x[cursorL] = x[cursorR];
          x[cursorR] = temp;
          Quicksort(x, von, bis);
        }
        else {
          int temp = x[cursorL];
          x[cursorL] = pivot;
          x[bis] = temp;  
        } // end of if-else  
        // end of if-else  
      } // end of if-else
    }
  }             
  public static int[] zahlen = new int[10];  
  public static int[] random (int[] x) {
    int z=0;
    for (int i=0; i<10; i++) {
      z = (int)(Math.random()*1000);
      zahlen[i]=z;
    } // end of for
    return x;
  }
  public static void main(String[] args) {
    //Quicksort:
    random(zahlen);  
    long t0 = System.nanoTime();
    Quicksort(zahlen, 0, 9);
    long t1 = System.nanoTime();
    System.out.println("Quicksort: " + Arrays.toString(zahlen));
    System.out.println("Zeit in Millisekunden: " + (t1-t0)*Math.pow(10,-6));
  }
}
 
Java:
while (pivot<x[cursorL] && cursorL <= bis) {
      cursorL++;
    } // end of while
    while (pivot>x[cursorR] && cursorR >= 0) {
      cursorR--;
    }
Du sortierst Werte, die größer als das pivot-Element sind, links ein, und Werte die kleiner sind rechts.

Übringens als ich eine seperate Tauschfunktion gemacht habe um ein paar Zeilen wegzulassen hab ich StackOverFlow errors bekommen.
Dann zeig doch mal, was du gemacht hast 😉
 

Zurück
Oben