Stuck in a simple quick sort implementation with random pivot

霸气de小男生 提交于 2019-12-06 05:08:44

You've made a small mistake in your recursion condition, both the start and end compares are against i, the start should be against j

You have:

    if (i - start > 1) {
        _sort(start, i);
    }
    if (end - i > 1) {
        _sort(i, end);
    }

Needs to be:

    if (j > start) {
        _sort(start, j);
    }
    if (end > i) {
        _sort(i, end);
    }

And your i and j comparison needs to allow equals:

   // here ----v
        if (i <= j) {
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
            i++;
            j--;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!