I have a recursion limit error in my quick sort program although I set recursion limit to 1500









up vote
-1
down vote

favorite












I am trying to write an in-place program that would use the quicksort algorithm to sort a list. But it keeps giving an error:




maximum recursion depth exceeded while calling python object.




Although I tried setting recursion limit.



def partition(L,low,high,comparison):
i=low-1
pivot=random_pivot(L)
for j in range(low,high):
if comparison(L[j],pivot):
i+=1
L[i],L[j]=L[j],L[i]
L[i+1],L[high]=L[high],L[i+1]
return i+1
def inplace_Quicksort(L,low,high,comparison):
low=0
high=len(L)-1
if comparison(low,high):
pivot_index=partition(L,low,high,comparison)
inplace_Quicksort(L,low,pivot_index-1,comparison)
inplace_Quicksort(L,pivot_index+1,high,comparison)
print(L)


Can you please have a look at it and explain to me what is wrong? Thank you so much.










share|improve this question























  • Try to debug your code yourself to see why the code recurses infinitely many times.
    – user202729
    Nov 10 at 16:16






  • 2




    Please be so good to not destroy the constructive edits made by others.
    – trincot
    Nov 10 at 16:16










  • What is random_pivot, and why do the arguments involve no range information?
    – user2357112
    Nov 10 at 16:17














up vote
-1
down vote

favorite












I am trying to write an in-place program that would use the quicksort algorithm to sort a list. But it keeps giving an error:




maximum recursion depth exceeded while calling python object.




Although I tried setting recursion limit.



def partition(L,low,high,comparison):
i=low-1
pivot=random_pivot(L)
for j in range(low,high):
if comparison(L[j],pivot):
i+=1
L[i],L[j]=L[j],L[i]
L[i+1],L[high]=L[high],L[i+1]
return i+1
def inplace_Quicksort(L,low,high,comparison):
low=0
high=len(L)-1
if comparison(low,high):
pivot_index=partition(L,low,high,comparison)
inplace_Quicksort(L,low,pivot_index-1,comparison)
inplace_Quicksort(L,pivot_index+1,high,comparison)
print(L)


Can you please have a look at it and explain to me what is wrong? Thank you so much.










share|improve this question























  • Try to debug your code yourself to see why the code recurses infinitely many times.
    – user202729
    Nov 10 at 16:16






  • 2




    Please be so good to not destroy the constructive edits made by others.
    – trincot
    Nov 10 at 16:16










  • What is random_pivot, and why do the arguments involve no range information?
    – user2357112
    Nov 10 at 16:17












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to write an in-place program that would use the quicksort algorithm to sort a list. But it keeps giving an error:




maximum recursion depth exceeded while calling python object.




Although I tried setting recursion limit.



def partition(L,low,high,comparison):
i=low-1
pivot=random_pivot(L)
for j in range(low,high):
if comparison(L[j],pivot):
i+=1
L[i],L[j]=L[j],L[i]
L[i+1],L[high]=L[high],L[i+1]
return i+1
def inplace_Quicksort(L,low,high,comparison):
low=0
high=len(L)-1
if comparison(low,high):
pivot_index=partition(L,low,high,comparison)
inplace_Quicksort(L,low,pivot_index-1,comparison)
inplace_Quicksort(L,pivot_index+1,high,comparison)
print(L)


Can you please have a look at it and explain to me what is wrong? Thank you so much.










share|improve this question















I am trying to write an in-place program that would use the quicksort algorithm to sort a list. But it keeps giving an error:




maximum recursion depth exceeded while calling python object.




Although I tried setting recursion limit.



def partition(L,low,high,comparison):
i=low-1
pivot=random_pivot(L)
for j in range(low,high):
if comparison(L[j],pivot):
i+=1
L[i],L[j]=L[j],L[i]
L[i+1],L[high]=L[high],L[i+1]
return i+1
def inplace_Quicksort(L,low,high,comparison):
low=0
high=len(L)-1
if comparison(low,high):
pivot_index=partition(L,low,high,comparison)
inplace_Quicksort(L,low,pivot_index-1,comparison)
inplace_Quicksort(L,pivot_index+1,high,comparison)
print(L)


Can you please have a look at it and explain to me what is wrong? Thank you so much.







python sorting quicksort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:44









Strawberryshrub

778215




778215










asked Nov 10 at 16:11









Asmaa Hadir

11




11











  • Try to debug your code yourself to see why the code recurses infinitely many times.
    – user202729
    Nov 10 at 16:16






  • 2




    Please be so good to not destroy the constructive edits made by others.
    – trincot
    Nov 10 at 16:16










  • What is random_pivot, and why do the arguments involve no range information?
    – user2357112
    Nov 10 at 16:17
















  • Try to debug your code yourself to see why the code recurses infinitely many times.
    – user202729
    Nov 10 at 16:16






  • 2




    Please be so good to not destroy the constructive edits made by others.
    – trincot
    Nov 10 at 16:16










  • What is random_pivot, and why do the arguments involve no range information?
    – user2357112
    Nov 10 at 16:17















Try to debug your code yourself to see why the code recurses infinitely many times.
– user202729
Nov 10 at 16:16




Try to debug your code yourself to see why the code recurses infinitely many times.
– user202729
Nov 10 at 16:16




2




2




Please be so good to not destroy the constructive edits made by others.
– trincot
Nov 10 at 16:16




Please be so good to not destroy the constructive edits made by others.
– trincot
Nov 10 at 16:16












What is random_pivot, and why do the arguments involve no range information?
– user2357112
Nov 10 at 16:17




What is random_pivot, and why do the arguments involve no range information?
– user2357112
Nov 10 at 16:17












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You are resetting the range to the complete list each time you call inplace_Quicksort in the first two lines of its code, thereby making the arguments low and high useless.



This leads to an endless recursion. No increase of maximum recursion depth will help you.



Instead give your arguments a default value. Look how it is done in this answer.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240830%2fi-have-a-recursion-limit-error-in-my-quick-sort-program-although-i-set-recursion%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You are resetting the range to the complete list each time you call inplace_Quicksort in the first two lines of its code, thereby making the arguments low and high useless.



    This leads to an endless recursion. No increase of maximum recursion depth will help you.



    Instead give your arguments a default value. Look how it is done in this answer.






    share|improve this answer
























      up vote
      1
      down vote













      You are resetting the range to the complete list each time you call inplace_Quicksort in the first two lines of its code, thereby making the arguments low and high useless.



      This leads to an endless recursion. No increase of maximum recursion depth will help you.



      Instead give your arguments a default value. Look how it is done in this answer.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        You are resetting the range to the complete list each time you call inplace_Quicksort in the first two lines of its code, thereby making the arguments low and high useless.



        This leads to an endless recursion. No increase of maximum recursion depth will help you.



        Instead give your arguments a default value. Look how it is done in this answer.






        share|improve this answer












        You are resetting the range to the complete list each time you call inplace_Quicksort in the first two lines of its code, thereby making the arguments low and high useless.



        This leads to an endless recursion. No increase of maximum recursion depth will help you.



        Instead give your arguments a default value. Look how it is done in this answer.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 16:18









        trincot

        112k1476108




        112k1476108



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240830%2fi-have-a-recursion-limit-error-in-my-quick-sort-program-although-i-set-recursion%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Museum of Modern and Contemporary Art of Trento and Rovereto

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully