Simple QuickSort Algorithm giving Stack Overflow Error?

安稳与你 提交于 2019-12-02 13:08:40

First, you have an infinite loop here:

while  (mitte<array[i]) {
    j--;
  } // end of if

It needs to be array[j]

Secondly (and leading to the infinite recursion), in the second call to quicksort

if (l<r) {
  quicksort(array,l,r);
} // end of if

In recursion, you always need to shorten the range that you call yourself with, or else it'll be infinite. I haven't worked out exactly what you're doing, but I think you mean:

if (i<r) {
   quicksort(array,i,r);
 } // end of if

This call:

if (l<r) {
  quicksort(array,l,r);
}

recursively calls quicksort with the same arguments that were passed in, rather than calling with a smaller subproblem to solve. It will therefore infinitely recurse.

if (l<r) 
quicksort(array,l,r);

Isnt l always less than r? this will cause an infinite recursion which is why you dont get an overflow if both values are the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!