473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 4228
On 28 May 2004 06:55:19 -0700
fl******@easy.com (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:<20040528082405.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
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...
2
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
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...
3
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...
7
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
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...
2
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"...
6
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
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...
0
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...

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.