-
Notifications
You must be signed in to change notification settings - Fork 6
/
qsort.h
68 lines (62 loc) · 1.45 KB
/
qsort.h
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
64
65
66
67
68
/**
* @file qsort.h
* @brief Sort function.
* @author Miguel I. Garcia Lopez / FloppySoftware
*
* This library implements a sort function of general use,
* which uses the bubble sort algorithm, for MESCC (Mike's Enhanced
* Small C Compiler for Z80 & CP/M).
*
* Revisions:
* - 30 Nov 2015 : First version (bubble sort).
* - 15 Aug 2016 : Documented. GPL v3.
*
* Copyright (c) 2015-2016 Miguel I. Garcia Lopez / FloppySoftware.
*
* Licensed under the GNU General Public License v3.
*
* http://www.floppysoftware.es
* floppysoftware@gmail.com
*/
#ifndef QSORT_H
#define QSORT_H
/**
* @fn void qsort(void *base, size_t items, size_t size, int (*comp)(const void *, const void*))
* @brief Sort an array of elements.
*
* Sort an array of elements into ascending order.
*
* The comparison function must return:
* - <0 on elem1 < elem2
* - =0 on elem1 == elem2
* - >0 on elem1 > elem2
*
* @param base - address of first element
* @param items - number of elements in the array
* @param size - size in bytes of each element
* @param comp - comparison function
*/
qsort(base, items, size, comp)
BYTE *base; int items, size; WORD comp;
{
int i, j, k;
BYTE *pi, *pj, t;
for(i = 0; i < items; ++i)
{
for(j = i + 1; j < items; ++j)
{
pi = base + i * size;
pj = base + j * size;
if(comp(pi, pj) > 0)
{
for(k = 0; k < size; ++k) {
t = *pi;
*pi++ = *pj;
*pj++ = t;
}
}
}
}
}
#endif