CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2005
    Posts
    4

    non-recursive quicksort

    can you give me a non recursive version of quicksort that uses a stack. the version should store at the stack indicies[i,j] of subarrays that have not been sorted yet.
    what is the number of [i,j] pairs, non-sorted subarrays that can belong to stack simultaneously.

  2. #2
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: non-recursive quicksort

    Any particular language? Here's one implementation in Java paraphrased from Robert Sedgewicks data structure's book:
    Code:
    static void quicksort(ITEM[] a, int l, int r) 
    {   Stack S = new Stack(50); 
        S.push(l); S.push(r); 
        
        while (!S.empty())
        { 
            r = S.pop(); l = S.pop(); 
            
            if (r <= l) continue; 
            
            int i = partition(a, l, r); 
            
            if (i-l > r-i) { S.push(l); S.push(i-1); } 
            
            S.push(i+1); S.push(r); 
            
            if (r-i >= i-l) { S.push(l); S.push(i-1); } 
        } 
    }
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured