cudd  3.0.0
The University of Colorado Decision Diagram Package
Data Structures | Macros | Functions
ucbqsort.c File Reference

Ancient implementation of qsort. More...

#include "util.h"
Include dependency graph for ucbqsort.c:

Data Structures

struct  info_t
 Miscellaneous information. More...
 

Macros

#define THRESH   4
 Threshold for insertion.
 
#define MTHRESH   6
 Threshold for median.
 

Functions

void util_qsort (void *vbase, int n, int size, QSFP compar)
 Implements the quicksort algorithm. More...
 
static void qst (char *base, char *max, info_t const *info)
 Do a quicksort. More...
 

Detailed Description

Ancient implementation of qsort.

This is shipped with CUDD so that results of reordering may be more reproducible across different platforms.

qsort.c 4.2 (Berkeley) 3/9/83

Our own version of the system qsort routine which is faster by an average of 25%, with lows and highs of 10% and 50%. The THRESHold below is the insertion sort threshold, and has been adjusted for records of size 48 bytes. The MTHREShold is where we stop finding a better median.

Function Documentation

◆ qst()

static void qst ( char *  base,
char *  max,
info_t const *  info 
)
static

Do a quicksort.

First, find the median element, and put that one in the first place as the discriminator. (This "median" is just the median of the first, last and middle elements). (Using this median instead of the first element is a big win). Then, the usual partitioning/swapping, followed by moving the discriminator into the right place. Then, figure out the sizes of the two partions, do the smaller one recursively and the larger one via a repeat of this code. Stopping when there are less than THRESH elements in a partition and cleaning up with an insertion sort (in our caller) is a huge win. All data swaps are done in-line, which is space-losing but time-saving. (And there are only three places where this is done).

◆ util_qsort()

void util_qsort ( void *  vbase,
int  n,
int  size,
QSFP  compar 
)

Implements the quicksort algorithm.

First, set up some global parameters for qst to share. Then, quicksort with qst(), and then a cleanup insertion sort ourselves. Sound simple? It's not...

Parameters
vbasestart address of array
nnumber of items
sizesize of each item
comparcomparison function