Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Quicksort algorithm is Causing stack overflow [modified]

Quicksort algorithm is Causing stack overflow [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresdatabasealgorithmshelp
11 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JKallen

    I do not understand why this is happening, however the following functions work for small numbers of data, but for larger numbers (ie 100,000 to 1,000,000) I get a stack overflow. These are protected functions of the class "Sample." Sample has a one dimensional dynamically allocated array of doubles pointed to by the m_data variable. Any help would be appreciated because I have not been able to find a solution to this Thanks void Sample::quicksort(signed long int top, signed long int bottom){ signed int long middle; if (top < bottom){ middle = partition(top, bottom); quicksort(top, middle); quicksort(middle+1, bottom); } return; } signed long int Sample::partition(signed long int top, signed long int bottom){ double x = *(m_data + top); signed long int i = top - 1; signed long int j = bottom + 1; double temp; do{ do{ j--; }while (x > *(m_data+j)); do{ i++; }while (x < *(m_data+i)); if (i < j){ temp = *(m_data+i); *(m_data+i) = *(m_data+j); *(m_data+j) = temp; } }while (i < j); return j; // returns middle index }

    PJ ArendsP Offline
    PJ ArendsP Offline
    PJ Arends
    wrote on last edited by
    #2

    You have recursive function calls. Every time a recursive function is called, the state of the caller is pushed onto the stack so it can be popped off the stack when the called function returns in order to restore the state. You are simply running out of stack space as a result.


    You may be right
    I may be crazy
    -- Billy Joel --

    Within you lies the power for good, use it!!!

    Within you lies the power for good; Use it!

    J 1 Reply Last reply
    0
    • J JKallen

      I do not understand why this is happening, however the following functions work for small numbers of data, but for larger numbers (ie 100,000 to 1,000,000) I get a stack overflow. These are protected functions of the class "Sample." Sample has a one dimensional dynamically allocated array of doubles pointed to by the m_data variable. Any help would be appreciated because I have not been able to find a solution to this Thanks void Sample::quicksort(signed long int top, signed long int bottom){ signed int long middle; if (top < bottom){ middle = partition(top, bottom); quicksort(top, middle); quicksort(middle+1, bottom); } return; } signed long int Sample::partition(signed long int top, signed long int bottom){ double x = *(m_data + top); signed long int i = top - 1; signed long int j = bottom + 1; double temp; do{ do{ j--; }while (x > *(m_data+j)); do{ i++; }while (x < *(m_data+i)); if (i < j){ temp = *(m_data+i); *(m_data+i) = *(m_data+j); *(m_data+j) = temp; } }while (i < j); return j; // returns middle index }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #3

      Any recursive function can cause a stack overflow if it recurses too often.

      JKallen wrote:

      signed int long middle;

      Is this legal ? It's certainly redundant. Variables are signed by default, and long and int are the same, so long would have done. Why is it there, this can't be C, it's a class. You've created a variable before it needs to exist ( and when sometimes it does not ), and not given it a default value. This is very poor form.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      J 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        You have recursive function calls. Every time a recursive function is called, the state of the caller is pushed onto the stack so it can be popped off the stack when the called function returns in order to restore the state. You are simply running out of stack space as a result.


        You may be right
        I may be crazy
        -- Billy Joel --

        Within you lies the power for good, use it!!!

        J Offline
        J Offline
        JKallen
        wrote on last edited by
        #4

        Ok. Why would that happen after a sort size of 3000 for instance????

        1 Reply Last reply
        0
        • C Christian Graus

          Any recursive function can cause a stack overflow if it recurses too often.

          JKallen wrote:

          signed int long middle;

          Is this legal ? It's certainly redundant. Variables are signed by default, and long and int are the same, so long would have done. Why is it there, this can't be C, it's a class. You've created a variable before it needs to exist ( and when sometimes it does not ), and not given it a default value. This is very poor form.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          J Offline
          J Offline
          JKallen
          wrote on last edited by
          #5

          It is legal. It is only redundant on a per compiler basis. long signed int specifies for all compilers where some platforms may default to short as the old C++ compilers do,..etc. the definition is fine. in fact I changed the "ints" to short signed unsigned lnog etc,...no difference. The problem is that I am running out of stack,...what i dont get is why this happenes sorting 3000 items.

          C 1 Reply Last reply
          0
          • J JKallen

            It is legal. It is only redundant on a per compiler basis. long signed int specifies for all compilers where some platforms may default to short as the old C++ compilers do,..etc. the definition is fine. in fact I changed the "ints" to short signed unsigned lnog etc,...no difference. The problem is that I am running out of stack,...what i dont get is why this happenes sorting 3000 items.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #6

            JKallen wrote:

            It is only redundant on a per compiler basis.

            Yes, it's true that the standard only sets minimum sizes for variable types, a long may be bigger than an int. But I don't see how 'long' wouldn't work everywhere ?

            JKallen wrote:

            in fact I changed the "ints" to short signed unsigned lnog etc,...no difference

            I know that, I was making a general comment about the efficiency and cleanness of the code.

            JKallen wrote:

            The problem is that I am running out of stack,...what i dont get is why this happenes sorting 3000 items.

            Obviously because each call is placing two more calls on the stack, and you're running out. What if you add a counter to the code, a static variable or something ? Perhaps the loop is running for more often than it needs to, and there's a bug in the partition code ?

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            J 1 Reply Last reply
            0
            • C Christian Graus

              JKallen wrote:

              It is only redundant on a per compiler basis.

              Yes, it's true that the standard only sets minimum sizes for variable types, a long may be bigger than an int. But I don't see how 'long' wouldn't work everywhere ?

              JKallen wrote:

              in fact I changed the "ints" to short signed unsigned lnog etc,...no difference

              I know that, I was making a general comment about the efficiency and cleanness of the code.

              JKallen wrote:

              The problem is that I am running out of stack,...what i dont get is why this happenes sorting 3000 items.

              Obviously because each call is placing two more calls on the stack, and you're running out. What if you add a counter to the code, a static variable or something ? Perhaps the loop is running for more often than it needs to, and there's a bug in the partition code ?

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              J Offline
              J Offline
              JKallen
              wrote on last edited by
              #7

              So. Is the message you cannot sort more than 3000 items using a quick sort algorithm? Is there a way around this limitation? ie is there a way to manage the recursive calls dynamically?

              C 1 Reply Last reply
              0
              • J JKallen

                So. Is the message you cannot sort more than 3000 items using a quick sort algorithm? Is there a way around this limitation? ie is there a way to manage the recursive calls dynamically?

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #8

                JKallen wrote:

                Is the message you cannot sort more than 3000 items using a quick sort algorithm?

                No, the message is that apparently you can't do that using this particular implimentation.

                JKallen wrote:

                ie is there a way to manage the recursive calls dynamically?

                Not that I know of. I believe you can change your stack size, but this is a poor solution IMO.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                1 Reply Last reply
                0
                • J JKallen

                  I do not understand why this is happening, however the following functions work for small numbers of data, but for larger numbers (ie 100,000 to 1,000,000) I get a stack overflow. These are protected functions of the class "Sample." Sample has a one dimensional dynamically allocated array of doubles pointed to by the m_data variable. Any help would be appreciated because I have not been able to find a solution to this Thanks void Sample::quicksort(signed long int top, signed long int bottom){ signed int long middle; if (top < bottom){ middle = partition(top, bottom); quicksort(top, middle); quicksort(middle+1, bottom); } return; } signed long int Sample::partition(signed long int top, signed long int bottom){ double x = *(m_data + top); signed long int i = top - 1; signed long int j = bottom + 1; double temp; do{ do{ j--; }while (x > *(m_data+j)); do{ i++; }while (x < *(m_data+i)); if (i < j){ temp = *(m_data+i); *(m_data+i) = *(m_data+j); *(m_data+j) = temp; } }while (i < j); return j; // returns middle index }

                  G Offline
                  G Offline
                  Gerald Schwab
                  wrote on last edited by
                  #9

                  Have a look at this implementation. It's optimized for speed and size. http://epaperpress.com/sortsearch/txt/qui.txt[^]

                  1 Reply Last reply
                  0
                  • J JKallen

                    I do not understand why this is happening, however the following functions work for small numbers of data, but for larger numbers (ie 100,000 to 1,000,000) I get a stack overflow. These are protected functions of the class "Sample." Sample has a one dimensional dynamically allocated array of doubles pointed to by the m_data variable. Any help would be appreciated because I have not been able to find a solution to this Thanks void Sample::quicksort(signed long int top, signed long int bottom){ signed int long middle; if (top < bottom){ middle = partition(top, bottom); quicksort(top, middle); quicksort(middle+1, bottom); } return; } signed long int Sample::partition(signed long int top, signed long int bottom){ double x = *(m_data + top); signed long int i = top - 1; signed long int j = bottom + 1; double temp; do{ do{ j--; }while (x > *(m_data+j)); do{ i++; }while (x < *(m_data+i)); if (i < j){ temp = *(m_data+i); *(m_data+i) = *(m_data+j); *(m_data+j) = temp; } }while (i < j); return j; // returns middle index }

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    I've not ever seen top/bottom used with quicksort. I use left/right. That aside... The reason for the endless recursion is that top is always LT bottom. See if this helps:

                    void Sample::quicksort( int top, int bottom )
                    {
                    if (top < bottom)
                    {
                    int nMiddle = partition(top, bottom);
                    quicksort(top, nMiddle - 1);
                    quicksort(nMiddle + 1, bottom);
                    }
                    }


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    1 Reply Last reply
                    0
                    • J JKallen

                      I do not understand why this is happening, however the following functions work for small numbers of data, but for larger numbers (ie 100,000 to 1,000,000) I get a stack overflow. These are protected functions of the class "Sample." Sample has a one dimensional dynamically allocated array of doubles pointed to by the m_data variable. Any help would be appreciated because I have not been able to find a solution to this Thanks void Sample::quicksort(signed long int top, signed long int bottom){ signed int long middle; if (top < bottom){ middle = partition(top, bottom); quicksort(top, middle); quicksort(middle+1, bottom); } return; } signed long int Sample::partition(signed long int top, signed long int bottom){ double x = *(m_data + top); signed long int i = top - 1; signed long int j = bottom + 1; double temp; do{ do{ j--; }while (x > *(m_data+j)); do{ i++; }while (x < *(m_data+i)); if (i < j){ temp = *(m_data+i); *(m_data+i) = *(m_data+j); *(m_data+j) = temp; } }while (i < j); return j; // returns middle index }

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #11

                      Algorithm Analysis [^]and QuickSort Algorithm[^] maybe it is some helpful to you

                      _**


                      **_

                      WhiteSky


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups