473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help in quicksort

how many recursive calls will quicksort make in the worst case for a file
of N items?
thank you

Nov 12 '06 #1
7 3322
zoro wrote:
how many recursive calls will quicksort make in the worst case for a file
of N items?
Wrong newsgroup: there's no "quicksort" in the C language.
Try a different newsgroup, like alt.do.your.own .stupid.homewor k
or or alt.careers.in. sewer.maintenan ce.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 12 '06 #2
Eric Sosman wrote:
zoro wrote:
>how many recursive calls will quicksort make in the worst case for a
file of N items?

Wrong newsgroup: there's no "quicksort" in the C language.
Try a different newsgroup, like alt.do.your.own .stupid.homewor k
or or alt.careers.in. sewer.maintenan ce.
Hmm, you might have misinterpreted the question, there is nothing on job
seeking or DIY, let alone specific to waste drainage and its upkeep.

Anyway, to answer the OP (or at least, partly): If you do the worst case
scenario on a piece of paper for a set of, say, five elements, you should be
easily able to figure this one out.

Or ask your question elsewhere. GIYF: quicksort recurse depth

--
Martijn
http://www.sereneconcepts.nl
http://blogger.xs4all.nl/mihaak/
Nov 12 '06 #3
In article <Nq************ *************** ***@comcast.com >,
Eric Sosman <es*****@acm-dot-org.invalidwrot e:
Wrong newsgroup: there's no "quicksort" in the C language.
An understandable mistake, given the unfortunate decision to call the
sort function "qsort()".

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 12 '06 #4
"zoro" <om********@nos pam.yahoo.comwr ote in message
news:f2******** *************** *******@localho st.talkaboutpro gramming.com...
: how many recursive calls will quicksort make in the worst case
: for a file of N items?
This depends on the implementation of the algorithm.
A naive implementation could have a worst case of N, but
a decent implementation (which qsort() should be in your
C library) will have a worst case of lg(N) recursions.
[ it is common for industrial strength implementations
to recurse on the smaller partition and iterate on
the larger one ]
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Nov 12 '06 #5
On Nov 12, 4:04 pm, "Ivan Vecerina"
<_INVALID_use_w ebfo...@ivan.ve cerina.comwrote :
"zoro" <omarzah...@nos pam.yahoo.comwr ote in messagenews:f2* *************** **************@ localhost.talka boutprogramming .com...
: how many recursive calls will quicksort make in the worst case
: for a file of N items?
This depends on the implementation of the algorithm.
A naive implementation could have a worst case of N, but
a decent implementation (which qsort() should be in your
C library) will have a worst case of lg(N) recursions.
While I see how you can limit the worst case _depth_ of recursion to
O(log N), the worst case _count_ of recursive calls is still O(N), no?
(The data arrangements that hit the worst cases for those two are
different, of course.)

I seem to recall a USENIX paper from 2000 or 2001 co-authored by Doug
McIlroy, in which the authors described a torture test for quicksort
which dynamically created a worst-case data arrangement for whatever
quicksort implementation it was given.
Philip Guenther

Nov 13 '06 #6

Philip Guenther wrote:
On Nov 12, 4:04 pm, "Ivan Vecerina"
<_INVALID_use_w ebfo...@ivan.ve cerina.comwrote :
"zoro" <omarzah...@nos pam.yahoo.comwr ote in messagenews:f2* *************** **************@ localhost.talka boutprogramming .com...
: how many recursive calls will quicksort make in the worst case
: for a file of N items?
This depends on the implementation of the algorithm.
A naive implementation could have a worst case of N, but
a decent implementation (which qsort() should be in your
C library) will have a worst case of lg(N) recursions.

While I see how you can limit the worst case _depth_ of recursion to
O(log N), the worst case _count_ of recursive calls is still O(N), no?
(The data arrangements that hit the worst cases for those two are
different, of course.)

I seem to recall a USENIX paper from 2000 or 2001 co-authored by Doug
McIlroy, in which the authors described a torture test for quicksort
which dynamically created a worst-case data arrangement for whatever
quicksort implementation it was given.
Bentley was the other author:
http://www.cs.ubc.ca/local/reading/p...1/spe862jb.pdf

There is an adversary set for any given input vector and partitioning
scheme.

The usual solution is to switch to heapsort when we have recursed
log(n) times and the sort has not finished. (Introspective sort does
this).

Probably, news:comp.progr amming was the most sensible place for this
question.
Philip Guenther
Nov 14 '06 #7
Philip Guenther wrote:
>
On Nov 12, 4:04 pm, "Ivan Vecerina"
<_INVALID_use_w ebfo...@ivan.ve cerina.comwrote :
"zoro" <omarzah...@nos pam.yahoo.com>
wrote in
messagenews:f28 a7b425be86675af 5f495cad5945e5@ localhost.
talkaboutprogra mming.com...
: how many recursive calls will quicksort make in the worst case
: for a file of N items?
This depends on the implementation of the algorithm.
A naive implementation could have a worst case of N, but
a decent implementation (which qsort() should be in your
C library) will have a worst case of lg(N) recursions.

While I see how you can limit the worst case _depth_ of recursion to
O(log N), the worst case _count_ of recursive calls is still O(N), no?
You are correct about that
for the kinds recursion schemes that Ivan Vecerina suggested
as in q0sort and q1sort,
but "This depends on the implementation of the algorithm."
is still true, since the quicksort algorithm
can also be implemented nonrecursively, as in q2sort.

#include <stddef.h>
#include <limits.h>

#define BYTE_SWAP(A, B) \
{ \
p1 = (A); \
p2 = (B); \
end = p2 + size; \
do { \
swap = *p1; \
*p1++ = *p2; \
*p2++ = swap; \
} while (p2 != end); \
}

void q0sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void*, const void*));
void q1sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void*, const void*));
void q2sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

void q0sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void*, const void*))
{
unsigned char *left;
unsigned char *p1, *p2, *end, swap;
size_t nmemb_right, middle, last, right;

if (nmemb-- 1) {
left = base;
right = last = nmemb * size;
middle = size;
while (compar(left, left + middle) 0 && middle != right) {
middle += size;
}
while (compar(left + last, left) 0) {
last -= size;
}
while (last middle) {
BYTE_SWAP(left + middle, left + last);
do {
middle += size;
} while (compar(left, left + middle) 0);
do {
last -= size;
} while (compar(left + last, left) 0);
}
BYTE_SWAP(left, left + last);
nmemb = last / size;
nmemb_right = (right - last) / size;
q0sort(last + size + left, nmemb_right, size, compar);
q0sort(base, nmemb, size, compar);
}
}

void q1sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void*, const void*))
{
unsigned char *left;
size_t nmemb_right, middle, last, right;
unsigned char *p1, *p2, *end, swap;

left = base;
while (nmemb-- 1) {
right = last = nmemb * size;
middle = size;
while (compar(left, left + middle) 0 && middle != right) {
middle += size;
}
while (compar(left + last, left) 0) {
last -= size;
}
while (last middle) {
BYTE_SWAP(left + middle, left + last);
do {
middle += size;
} while (compar(left, left + middle) 0);
do {
last -= size;
} while (compar(left + last, left) 0);
}
BYTE_SWAP(left, left + last);
nmemb = last / size;
nmemb_right = (right - last) / size;
if (nmemb_right nmemb) {
q1sort(left, nmemb, size, compar);
left += last + size;
nmemb = nmemb_right;
} else {
q1sort(left + last + size, nmemb_right, size, compar);
}
}
}

void q2sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *left;
size_t middle, last, right;
struct {
size_t bytes;
void *base;
} stack[CHAR_BIT * sizeof nmemb], *stack_ptr;
unsigned char *p1, *p2, *end, swap;

stack -bytes = nmemb * size;
stack -base = base;
stack_ptr = stack + 1;
do {
--stack_ptr;
if (stack_ptr -bytes size) {
left = stack_ptr -base;
right = last = stack_ptr -bytes - size;
middle = size;
while (compar(left, left + middle) 0
&& middle != right)
{
middle += size;
}
while (compar(left + last, left) 0) {
last -= size;
}
while (last middle) {
BYTE_SWAP(left + middle, left + last);
do {
middle += size;
} while (compar(left, left + middle) 0);
do {
last -= size;
} while (compar(left + last, left) 0);
}
BYTE_SWAP(left, left + last);
if (right - last last) {
stack_ptr - base = left + last + size;
stack_ptr++ -bytes = right - last;
stack_ptr - base = left;
stack_ptr++ -bytes = last;
} else {
stack_ptr++ -bytes = last;
stack_ptr - base = left + last + size;
stack_ptr++ -bytes = right - last;
}
}
} while (stack_ptr != stack);
}

--
pete
Nov 14 '06 #8

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

Similar topics

5
5225
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
2407
by: Lieven | last post by:
I want to write a generic quicksort over STL containers. We have to parallelize this algorithm afterwards. This is what I've got for now: template<typename containerIterator> void quicksort(containerIterator begin, containerIterator end){         if (end > begin){                 containerIterator pivot(begin);                 containerIterator newPivot(partition(begin, end, pivot));
3
3572
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>...
2
2936
by: nkharan | last post by:
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, low, high) recursive Input: L (a list of size n), low, high (integers) Output: L is sorted if high > low then
2
2159
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
7615
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
15483
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
6029
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.
0
8407
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
8837
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...
1
8512
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
8612
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...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
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
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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.