[Solved] C++ quicksort in one function with 2 parameters (___ _____, int length)


Quick sort is in-place manipulation. Means you will be modifying original array only. No need to create extra arrays. This is answer of your both questions.

Try this code

void qsort(int* xArray, int xSize)
{
        int lPivot = xArray[xSize-1];
        int lIndexOfLargestElement = 0;
        for (int i = 0; i < xSize-1; i++)
        {
                if (xArray[i] < lPivot)
                {
                        // Swap largest element with this
                        int lTmp = xArray[i];
                        xArray[i] = xArray[lIndexOfLargestElement];
                        xArray[lIndexOfLargestElement]  = lTmp;
                        lIndexOfLargestElement++;
                }
        }
        // swap pivot with xArray[lIndexOfLargestElement]
        int lTmp = xArray[lIndexOfLargestElement];
        xArray[lIndexOfLargestElement] = xArray[xSize-1];
        xArray[xSize-1] = lTmp;
        if (lIndexOfLargestElement > 1)
                qsort(xArray, lIndexOfLargestElement);
        if (xSize-lIndexOfLargestElement-1 > 1)
                qsort(xArray+lIndexOfLargestElement+1, xSize-lIndexOfLargestElement-1);
}

0

solved C++ quicksort in one function with 2 parameters (___ _____, int length)