Questions about Quick Sort with the middle pivot

Hello everybody. Please help me with the following problem:

I am learning algorithm Quick Sort. My teacher said that if I choose the middle pivot, there are arrays that many repeating elements will give wrong results.
But I have tried a lot of arrays have many repeat like that but have not seen any cases that give wrong results.

Explain to help me with what my teacher said.
Thank you very much.

This is my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void qs(int a[], int l, int r)
{
    int p=a[(l+r)/2], i=l, j=r, t;
    while(i<=j)
    {
        while(a[i]<p)
            i++;
        while(a[j]>p)
            j--;
        if(i<=j)
        {
            t=a[i] ;
            a[i]=a[j];
            a[j]=t;
            i++;
            j--;
        }
    }
    if(l<j)
        qs(a,l,j);
    if(i<r)
        qs(a,i,r);
}
Last edited on
https://en.wikipedia.org/wiki/Quicksort#Repeated_elements
I don't think the result is wrong.
But it can become very inefficient with many repeated elements.

Thanks salem c .

Or, is there any problem with the above algorithm? A my friend said, tried this algorithm with many repeated numbers and encountered a wrong case, and I haven't seen it before...

Last edited on
Like the link says, it just becomes inefficient.

> A my friend said, tried this algorithm with many repeated numbers and encountered a wrong case
Well get them to tell you what that case was.
Today, I met him again, watched the source code he wrote, and discovered, there was a bad error.
Thanks very much to salem c.
Last edited on
Topic archived. No new replies allowed.