-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqsort.c
115 lines (102 loc) · 2.59 KB
/
qsort.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
int values[] = { 100,
10,
1,
0x80000000,
0x70000000, };
/**
* Description
*
* The naïve number sorting function (x - y) fails for many big
* positive and negative integers because of integer overflow.
*
* To understand why this happens, lets look back at 2s complement
* arithmetic.
*
* 2s complement subtraction is done via normal addition with
* the right hand operand being negated. e.g.:
*
* x - y = x + (-y)
*
* Negation is a two step process. First all bits are flipped and
* then the resulting integer is summed with 1:
*
* -y = (~y) + 1
*
* Example:
* 5 - 3 == 5 + (-3)
*
* 5 == 0101
* 3 == 0011
*
* -3 == ~0011 + 0001 -> 1100
* 0001
* ----
* 1101
*
* 5 + (-3) == 0101
* 1101
* ----
* 1|0010
*
* result: 0010 == 2
*
*
* Why does compare fail?
* ----------------------
*
* Consider (16 bit but applies equally well to 32 bits):
* 0x8000 == 1000 0000 0000 0000 (binary)
* 0x7000 == 0111 0000 0000 0000 (binary)
*
* -0x7000 == 1000 1111 1111 1111
* 0000 0000 0000 0001
* -------------------
* 1001 0000 0000 0000
*
* 0x8000 + (-0x7000) == 1000 0000 0000 0000
* 1001 0000 0000 0000
* 1|0001 0000 0000 0000
* ^
* overflow
*
*
* 0x1000 is a positive number! Because of the overflow, the
* sign bit gets lost, thus (the negative) 0x8000 looks bigger
* than (the positive) 0x7000!
*
* Conclusion:
* Don't use clever tricks! Use the natural solution for the
* problem at hand. In this case it's relational operators :)
*
**/
int compareInt(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int properCompareInt(const void *a, const void *b)
{
int x = *(int *)a;
int y = *(int *)b;
return (x == y) ? 0
: (x > y) ? 1 : -1;
}
int main ()
{
int n;
printf("Bad compare:\n");
qsort (values, LENGTH(values), sizeof(int), compareInt);
for (n=0; n < LENGTH(values); n++) {
printf ("%d ",values[n]);
}
printf("\n\n");
printf("Good compare:\n");
qsort (values, LENGTH(values), sizeof(int), properCompareInt);
for (n=0; n < LENGTH(values); n++) {
printf ("%d ",values[n]);
}
printf("\n\n");
return 0;
}