Hey everybody! I’m trying to make a really stupid sorting algorithm as a joke, but I can’t seem to get it to work. It’s called “Supernatural Occurence Sort”, and basically, my idea was as follows:
if nArr is sorted somehow
sorted = true and we're done
if nArr is not sorted
if the current value is less than/equal to the next value
go to the next value
if the current value is greater than the next value
swap the current value with a random value in the list
if we're at the end of the list and all the numbers are somehow in the right order
sorted = true and we're done
if we're at the end of the list and the numbers are not in the right order
call the method again
So it says in the console a timeout occured at [a random packet] each time I run the code, so i’m not sure what i’m doing wrong. Any tips? My code is down below:
int[] nArr = new int[10];
boolean soFarSoGood = false;
boolean sorted = false;
void setup() {
for (int i = 0; i < nArr.length; i++) {
nArr[i] = (int)random(1, 10);
}
supernaturalOccurenceSort();
}
void draw() {
for (int i = 0; i < nArr.length; i++) {
print(nArr[i] + " ");
}
println("Sorted: " + sorted + " SFSG: " + soFarSoGood);
}
void supernaturalOccurenceSort() {
for (int i = 0; i < nArr.length; i++) {
if (i < nArr.length - 1 ) {
if (nArr[i] >= nArr[i+1] && soFarSoGood == true) {
soFarSoGood = true;
}
if (nArr[i] < nArr[i+1]) {
soFarSoGood = false;
swap(nArr[i], nArr[(int)random(1, 10)]);
}
}
if (i >= nArr.length-1) {
if (soFarSoGood == true) {
sorted = true;
}
}
}
if (sorted == false) {
supernaturalOccurenceSort();
}
}
void swap(int a, int b) {
int temp = nArr[a];
nArr[a] = nArr[b];
nArr[b] = temp;
}