473,785 Members | 3,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing Tail recursions in quicksort

Hey,
Here is the pseudocode of quicksort. It can be observed that there
are as many as (n-1) unresolved stack calls in the worst case. Now, how
do i reduce this unresolved stack calls? what modifications should I
make?

procedure QuickSort(L[0:n-1], low, high) recursive
Input: L[0:n-1] (a list of size n), low, high (integers)
Output: L[low:high] is sorted
if high > low then
Partition(L[0:n-1], low, high, position)
QuickSort(L[0:n-1], low, position - 1)
QuickSort(L[0:n-1], position + 1, high)
endif
end QuickSort
thanks,

Haran

Oct 19 '05 #1
2 2944

nk*****@gmail.c om wrote:
Hey,
Here is the pseudocode of quicksort. It can be observed that there
are as many as (n-1) unresolved stack calls in the worst case. Now, how
do i reduce this unresolved stack calls? what modifications should I
make?

procedure QuickSort(L[0:n-1], low, high) recursive
Input: L[0:n-1] (a list of size n), low, high (integers)
Output: L[low:high] is sorted
if high > low then
Partition(L[0:n-1], low, high, position)
QuickSort(L[0:n-1], low, position - 1)
QuickSort(L[0:n-1], position + 1, high)
endif
end QuickSort


I would expect only log2(n) levels of recursion for a quicksort of n
items. So I would change the pseudocode to be a less recursive
implementation.

Greg

Oct 19 '05 #2
nk*****@gmail.c om wrote:
Hey,
Here is the pseudocode of quicksort. It can be observed that there
are as many as (n-1) unresolved stack calls in the worst case. Now, how
do i reduce this unresolved stack calls? what modifications should I
make?

procedure QuickSort(L[0:n-1], low, high) recursive
Input: L[0:n-1] (a list of size n), low, high (integers)
Output: L[low:high] is sorted
if high > low then
Partition(L[0:n-1], low, high, position)
QuickSort(L[0:n-1], low, position - 1)
QuickSort(L[0:n-1], position + 1, high)
endif
end QuickSort
thanks,

Haran


Obvious homework question and nothing to do with C++ either.

Obviously you weren't paying to much attention in class, had you been
you would have known to convert the tail recursive call into a loop.

john
Oct 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5233
by: why | last post by:
Hi, just been curious. i have learn that quicksorting algorithm is more widely used then heapsort, despite having the same performance indication of O(n log n) anyone knows why? newbie
3
3579
by: Lieven | last post by:
I want to make a quicksort algorithm that can take both a vector and a list. This is the code I've got. But I also want to give an argument that will be used for comparing the elements. I am used to programming in Lisp/Scheme, where I would just pass a lambda. Is a function object something like that in C++? And in that case, how would I pass the function/operator < (lesser then). //quicksort template<typename BidirectionalIterator>...
4
2115
by: Crutcher | last post by:
This is fun :) {Note: I take no responsibilty for anyone who uses this in production code} #!/usr/bin/env python2.4 # This program shows off a python decorator # which implements tail call optimization. It # does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack.
2
2165
by: camotito | last post by:
Hi, I want to sort an array of pointers, each position of this array point to a position in an integer array. There is a strange behavior when the 'pivot' is '0'. I am using Visual C++, and when executing the OS tell me "quicksort.exe has encountered a problem and needs to close". When there is no zeros in the integer array, this doesn't happen. The pivot is always the last element. Try to change the last element in the array for a non...
2
7631
by: Sendell | last post by:
Sorry that this is such a trivial question... For an assignment I had to write the quicksort program: //This program sorts a set of integers using the quicksort method. #include "stdafx.h" #include "genlib.h" #include "simpio.h" #include <stdio.h>
6
944
by: Baltazar007 | last post by:
Does anyone know how to make quicksort for single linked list without using array? I know that mergesort is maybe better but I need quicksort!! thnx
2
15495
by: simo | last post by:
Hello to all... I am trying to write an algorithm parallel in order to realize the quicksort. They are to the first crews with C and MPI. The algorithm that I am trying to realize is PARALLEL QUICKSORT with REGULAR SAMPLING. The idea on the planning is right. the code that I have written syntactically does not only have problems that it does not want any to know to work... Are two days that I analyze it but I do not succeed to find the...
8
6036
by: aparnakakkar2003 | last post by:
hello can any one tell me how i can create program to sort string list(standard template library) using quicksort.
35
4741
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.