0

I have implemented the quicksort algorithm using the last element as the pivot. Now I want to generate an array for the best-case scenario. This is the function that I wrote, but I am not sure that it works properly. Note: values are already sorted in increasing order, efficiency of the generateBestCase is not important

void generateBestCase(int *values, int left, int right) {
        if (left>=right) return;
        int mid = (left+right)/2;
        swap(values[mid], values[right]);
        generateBestCase(values, left, mid-1);
        generateBestCase(values, mid+1, right-1);
}
8
  • 1
    The best case for Quicksort is the already sorted array because in that case no swapping occurs (except for the pivot in this variant). Incidentally, shouldn't you swap the pivot element back to the center after the recursion? Commented Oct 25 at 18:05
  • 2
    int mid = left+(right-left)/2; Commented Oct 25 at 19:38
  • 5
    "I am not sure that it works properly": that is not a problem statement though. What makes you doubt? Did you test? Was there a case that failed? How can we reproduce the problem? Commented Oct 25 at 20:54
  • 1
    For more info about @Eljay's comment above see: why left+(right-left)/2 will not overflow?. Commented Oct 26 at 6:30
  • 2
    @500-InternalServerError You should count comparisons, not swaps. Commented Oct 26 at 8:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.