|
-
March 18th, 2005, 07:04 PM
#1
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.
-
March 20th, 2005, 02:59 PM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|