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. ATL / WTL / STL
  4. How to prevent Quicksort stack overflow?

How to prevent Quicksort stack overflow?

Scheduled Pinned Locked Moved ATL / WTL / STL
algorithmsdata-structureshelptutorialquestion
2 Posts 2 Posters 0 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.
  • C Offline
    C Offline
    crazy66
    wrote on last edited by
    #1

    My aim is to learn the data structure, in the QuickSort algorithm . 0.6s to complete the order that is 1 million random numbers. 1. However, if the rand number such as rand ()% 2, such a random number will be occur stack overflow , the problem is very difficult for me , in order to keeping the rapid nature of QuickSort, may I ask how to solve the stack overflow problem. 2. But luckily no stack overflow if the situation is still rand ()% 2, such a random number calculate the time to reach 17s. Thank you very much. Attached on the source code:

    void SwitchNum(int& n1,int& n2)
    {
    int nTemp=n1;
    n1=n2;
    n2=nTemp;
    }

    void QuickSort(int *pnArray,int nSize)
    {
    if (nSize>1)
    {
    int nNum=nSize/2,i=0,j=nSize-1;
    while (i!=j)
    {
    for (;j>nNum;j--)
    {
    if (pnArray[nNum]>pnArray[j])
    {
    SwitchNum(pnArray[nNum],pnArray[j]);
    nNum=j;
    break;
    }
    }
    for (;i <nNum;i++)
    {
    if (pnArray[nNum] <pnArray[i])
    {
    SwitchNum(pnArray[nNum],pnArray[i]);
    nNum=i;
    break;
    }
    }
    }
    QuickSort(pnArray,nNum);
    QuickSort(pnArray+nNum+1,nSize-nNum-1);
    }
    }

    L 1 Reply Last reply
    0
    • C crazy66

      My aim is to learn the data structure, in the QuickSort algorithm . 0.6s to complete the order that is 1 million random numbers. 1. However, if the rand number such as rand ()% 2, such a random number will be occur stack overflow , the problem is very difficult for me , in order to keeping the rapid nature of QuickSort, may I ask how to solve the stack overflow problem. 2. But luckily no stack overflow if the situation is still rand ()% 2, such a random number calculate the time to reach 17s. Thank you very much. Attached on the source code:

      void SwitchNum(int& n1,int& n2)
      {
      int nTemp=n1;
      n1=n2;
      n2=nTemp;
      }

      void QuickSort(int *pnArray,int nSize)
      {
      if (nSize>1)
      {
      int nNum=nSize/2,i=0,j=nSize-1;
      while (i!=j)
      {
      for (;j>nNum;j--)
      {
      if (pnArray[nNum]>pnArray[j])
      {
      SwitchNum(pnArray[nNum],pnArray[j]);
      nNum=j;
      break;
      }
      }
      for (;i <nNum;i++)
      {
      if (pnArray[nNum] <pnArray[i])
      {
      SwitchNum(pnArray[nNum],pnArray[i]);
      nNum=i;
      break;
      }
      }
      }
      QuickSort(pnArray,nNum);
      QuickSort(pnArray+nNum+1,nSize-nNum-1);
      }
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      crazy66 wrote:

      may I ask how to solve the stack overflow problem.

      QuickSort() is a recursive procedure, so I can only guess that your recursion goes to too deep a level, hence stack overflow.

      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