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 ?

  • 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. – MK. Oct 26 '15 at 22:21
  • Upvoted. However, sure not the best way, plus this famous algorithm will have dozens of perfect implementations.. –  Oct 26 '15 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. – juanchopanza Oct 26 '15 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. –  Oct 27 '15 at 00:18
  • 1
    @YangYing, here is one [link](http://stackoverflow.com/questions/18262306/quick-sort-with-python/20258416#20258416), talking some implementations of quick sort. – zangw Oct 27 '15 at 00:46
  • @juanchopanza well there are two things that SO does which are both valid in my opinion and one does not hurt the other. One is collecting generally useful solutions to common and not so common problems. The other is helping people. This is an instance of (2). – MK. Oct 27 '15 at 03:35
  • This actually really helped me because I wanted to see a comparison of how the same code looks for both languages. – Joffrey Baratheon Jan 25 '18 at 20:47

1 Answers1

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.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • 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 :/ –  Oct 26 '15 at 22:12