File: qsort.c

package info (click to toggle)
espresso 6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 254,572 kB
  • sloc: f90: 407,419; sh: 41,218; xml: 28,535; ansic: 27,880; tcl: 18,512; makefile: 4,265; python: 3,643; cpp: 761; fortran: 618; java: 568; perl: 272; awk: 57; lisp: 15
file content (63 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
  Copyright (C) 2002 FPMD group
  This file is distributed under the terms of the
  GNU General Public License. See the file `License'
  in the root directory of the present distribution,
  or http://www.gnu.org/copyleft/gpl.txt .
*/

#include<stdio.h>
#include<stdlib.h>

/* qsort - quick sort

   qsort(n,comp,swap)
   unsigned n;
   int (*comp)();
   int (*swap)();
                                  ***** see bsort for parameters

*/

static unsigned _rearr(unsigned lb,unsigned ub);
static void _quick(unsigned lb,unsigned ub);
static int (*_comp)(unsigned,unsigned), (*_swap)(unsigned,unsigned);

void Qsort(unsigned n,int (*comp)(),int (*swap)())
{
        _comp = comp;
        _swap = swap;
        _quick(0,n-1);
}


static void _quick(unsigned lb,unsigned ub)
{
unsigned j;

if(lb<ub)
        {
        if((j = _rearr(lb,ub)))
        _quick(lb,j-1);
        _quick(j+1,ub);
        }
}


static unsigned _rearr(unsigned lb,unsigned ub)
{
do
        {
        while(ub > lb && (*_comp)(ub,lb) >=0)   ub--;

        if(ub != lb)
                {
                (*_swap)(ub,lb);
                while(lb<ub && (*_comp)(lb,ub)<=0) lb++;
                if(lb != ub)    (*_swap)(lb,ub);
                }
        } while(lb != ub);

return lb;
}