Quicksort : StackOverflow Error : This sketch is attempting too much recursion
in
Programming Questions
•
2 years ago
Hello all,
I've written a sketch that uses the quicksort algorithm to sort a load of pixels but im getting an error on large pictures,
is there any workarounds for this, tried increasing memory allocation but no use.
- void quickSort(int lo0, int hi0) {
- int lo = lo0;
- int hi = hi0;
- if (lo >= hi) {
- return;
- }
- else if ( lo == hi - 1 ) {
- if (bPixels[lo] > bPixels[hi]) {
- float T = bPixels[lo];
- color S = p.pixels[lo];
- bPixels[lo] = bPixels[hi];
- p.pixels[lo] = p.pixels[hi];
- bPixels[hi] = T;
- p.pixels[hi] = S;
- }
- return;
- }
- float pivot = bPixels[(lo+hi) / 2];
- bPixels[(lo + hi) / 2] = bPixels[hi];
- bPixels[hi] = pivot;
- while( lo < hi) {
- while(bPixels[lo] <= pivot && lo < hi ) {
- lo++;
- }
- while (pivot <= bPixels[hi] && lo < hi) {
- hi--;
- }
- if (lo < hi) {
- float T = bPixels[lo];
- color S = p.pixels[lo];
- bPixels[lo] = bPixels[hi];
- p.pixels[lo] = p.pixels[hi];
- bPixels[hi] = T;
- p.pixels[hi] = S;
- }
- }
- bPixels[hi0] = bPixels[hi];
- bPixels[hi] = pivot;
- quickSort(lo0,lo -1);
- quickSort(hi+1, hi0);
- }
i got the code for the algorithm from
here
roboguy
2