4

I am trying to implement quick sort algorithm (https://en.wikipedia.org/wiki/Quicksort) in Python. I successfully implemented the algorithm in C++, but it is giving weird results in Python.

Here is the C++ code:

//Quicksort implementation
//Anchit Virmani - 27/10/2015 02:07 AM
#include <iostream>
using namespace std;

void swap(int* a, int* b)
{
    int t=*a;
    *a=*b;
    *b=t;
}

void quick_sort(int arr[],int l, int r)
{
    if(l<r)
    {
        int p=arr[l];
        int i=l+1;

        for(int j=(l+1);j<=r;j++)
        {
            if(arr[j]<p)
            {
                swap(&arr[j],&arr[i]);
                i++;
            }
        }
        swap(&arr[l],&arr[i-1]);

        quick_sort(arr,l,i-2);
        quick_sort(arr,i,r);

    }
}
int main() {
    int arr[3]={2,1,3};
    quick_sort(arr,0,2);

    for(int i=0;i<3;i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
}

And this is the code I wrote in Python :

def quick_sort(arr,l,r):
    if(l<r):
        p=arr[l]
        i=l+1
        j=0

        for j in range(l+1,r+1):
            if arr[j]<p:
                arr[j],arr[i]=arr[i],arr[j]
                i=i+1


        arr[l],arr[r]=arr[r],arr[l]

        quick_sort(arr,l,i-2)
        quick_sort(arr,i,r)



arr=[4,3,2,1]
quick_sort(arr,0,3)

print(arr)

What is wrong with the Python implementation ?

7
  • OMG what's with the downvote? Sure, this is somewhat of a silly question and it won't help anyone else, but at least it is clear what he is asking and he provided all the code needed to help him. Commented Oct 26, 2015 at 22:21
  • Upvoted. However, sure not the best way, plus this famous algorithm will have dozens of perfect implementations.. Commented Oct 26, 2015 at 23:20
  • 1
    @MK. It is a "fix my bugs for me" code dump. "Somewhat of a silly question and it won't help anyone else" indeed. The surprise is that is is up-voted. With voting patterns like this, it is no surprise this site is rapidly filing up with crap. Commented Oct 26, 2015 at 23:33
  • 1
    @juanchopanza It wouldn't have been a "fix my bugs for me" question , if I had not provided the C++ code. Actually, I am new to python, so I was not sure if I was doing everything fine in Python, hence I came to SO for help. Commented Oct 27, 2015 at 0:18
  • 1
    @YangYing, here is one link, talking some implementations of quick sort. Commented Oct 27, 2015 at 0:46

1 Answer 1

1

Well, if you compare C++ and Python versions line by line you will notice that this line

 arr[l],arr[r]=arr[r],arr[l]

in Python differs from C++. Also your choice of pivot point seems weird.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the pivot is strange because I need to select the first element of array for a subsequent sub task in homework question. And thanks for pointing out the mistake, I feel so dumb to not notice this :/

Your Answer

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