Hi ich muss den Slowsort Algorithmus umsetzen, und bekomme dabei einen Fehler, den ich irgendwie nicht vermeiden kann.
Ich hab mich an den Pseudocode gehalten, den es auf Wikipedia gibt, und habe den Algorithmus auch durchgespielt, ich komme trotzdem nicht drauf, warum ich den StackOverflow bekomme. Ich hoffe es hat jemand einen Tipp
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sortalgo;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author ich
*/
public class SlowSort {
private int[] num;
private int numb;
public void sort(int[] values) {
if (values == null || values.length == 0) {
return;
}
this.num = values;
numb = values.length;
slowsort(values,0, numb-1);
}
private void slowsort(int[] values, int i, int j) {
if (values[i] >= values[j]) {
int m = (i + j) / 2;
slowsort(values, i, m);
slowsort(values, m + 1, j);
if (values[j] < values[m]) {
int temp = num[j];
num[j] = num[m];
num[m] = temp;
}
}
slowsort(values, i, j - 1);
}
public void random(int[] array) {
for (int k = 0; k < array.length; k++) {
Random r = new Random();
int rand = r.nextInt();
array[k] = rand;
}
}
public static void main(String[] args) {
int [] values;
values = new int [3];
SlowSort slow = new SlowSort();
slow.random(values);
slow.sort(values);
System.out.println(Arrays.toString(values));
}
}
Code:
Exception in thread "main" java.lang.StackOverflowError
at sortalgo.SlowSort.slowsort(SlowSort.java:30)
at sortalgo.SlowSort.slowsort(SlowSort.java:32)
at sortalgo.SlowSort.slowsort(SlowSort.java:32)
at sortalgo.SlowSort.slowsort(SlowSort.java:32)
at sortalgo.SlowSort.slowsort(SlowSort.java:32)
Ich hab mich an den Pseudocode gehalten, den es auf Wikipedia gibt, und habe den Algorithmus auch durchgespielt, ich komme trotzdem nicht drauf, warum ich den StackOverflow bekomme. Ich hoffe es hat jemand einen Tipp