473,804 Members | 3,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reversing order of quicksort

Martin Dickopp <ex************ ****@zero-based.org> wrote in message news:<cu******* ******@zero-based.org>...

is there
any particular reason why you cannot or don't want to use the `qsort'
function in the standard C library?
I'm using gcc on mingw and assumed it was so minimal it wouldn't
include it. Should've checked because it does of course.
As to your question, just invert all comparisons of the elements to be
sorted. For example,
while (item[i]>x && i<right) i++;
while (x>item[j] && j>left) j--;


should become

while (item[i]<x && i<right) i++;
while (x<item[j] && j>left) j--;


Thanks very much - I couldn't quite see what to invert and what to
leave alone. That works perfectly.
Nov 14 '05 #1
2 4267
On 28 May 2004 06:55:19 -0700
fl******@easy.c om (flipflop) wrote:
Martin Dickopp <ex************ ****@zero-based.org> wrote in message news:<cu******* ******@zero-based.org>...

is there
any particular reason why you cannot or don't want to use the `qsort'
function in the standard C library?


I'm using gcc on mingw and assumed it was so minimal it wouldn't
include it. Should've checked because it does of course.


There's a very good reason not to use the built in qsort functions -
much better performance can be achieved by using a custom built quicksort routine that doesn't pass the
comparison as a pointer to a function. If your
program is rate limited by the sort function the overhead in that
method can result in a >2X speed difference. For instance, on an
Athlon MP2000 sorting a 10M cell integer array with gcc 3.3.3 qsort
takes 6.4 seconds but using my own quicksort, with integer comparison
built in (not a function call), it runs in 2.7 seconds. The ratio
is similar for other small data types and only approaches 1x when
comparing large or complex data types (ie, much more time in the comparison function than in the function call overhead).

That said, very few programs I've encountered are rate limited by sorts,
and the builtin qsort is more convenient in that you don't need a different qsort for each data type sorted, just a different comparison
function (which is typically small and easy to write). If your program
is only spending 2% of its time sorting then doubling the sorting speed
will only speed up the program by 1%, which is pretty negligible.

Regards,

David Mathog
ma****@caltech. edu
Manager, Sequence Analysis Facility, Biology Division, Caltech
Nov 14 '05 #2
David Mathog <ma****@caltech .edu> wrote in message news:<200405280 82405.5a781700. ma****@caltech. edu>...
There's a very good reason not to use the built in qsort functions -
much better performance can be achieved by using a custom built quicksort routine that doesn't pass the
comparison as a pointer to a function. If your
program is rate limited by the sort function the overhead in that
method can result in a >2X speed difference. For instance, on an
Athlon MP2000 sorting a 10M cell integer array with gcc 3.3.3 qsort
takes 6.4 seconds but using my own quicksort, with integer comparison
built in (not a function call), it runs in 2.7 seconds. The ratio
is similar for other small data types and only approaches 1x when
comparing large or complex data types (ie, much more time in the comparison function than in the function call overhead).

That said, very few programs I've encountered are rate limited by sorts,
and the builtin qsort is more convenient in that you don't need a different qsort for each data type sorted, just a different comparison
function (which is typically small and easy to write). If your program
is only spending 2% of its time sorting then doubling the sorting speed
will only speed up the program by 1%, which is pretty negligible.

While this is all perhaps true, the OP's implementation is quite poor
(fully recursive, no median-of-three, uses Quicksort even on tiny
partition, etc.). Assuming it runs at all (eg. no stack overflow on
largish inputs), it'll probably get trashed performance-wise by the C
library routine, despite the overhead of the indirect call to the
comparison function.
Nov 14 '05 #3

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

Similar topics

3
3581
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
8399
by: Luigi Napolitano | last post by:
Hello, I use this algorithm to sort an array "v" in the descending order. void quickSort(float v, int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) return; float mid = v;
2
2945
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
3
551
by: flipflop | last post by:
Sorry if this is a very faq - I haven't seen the answer here though. The quicksort code below sorts an array from large to small. I simply want it to reverse the sort order: small to large, but can't see how. I've walked through it on paper but am just not seeing it. qsort(double *item,int array_size) { qsort_recurse(item,0,array_size-1); }
7
3758
by: Luigi Napolitano | last post by:
Hello, I use this algorithm to sort an array "v" in the descending order. void quickSort(float v, int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) return; float mid = v;
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
7632
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
8
4765
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
0
9708
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
10340
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
10324
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
9161
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...
1
7623
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
5527
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...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
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.