Skip to content

Commit 0bba382

Browse files
committed
Change to insertion sort
In theory faster, but the effect is minimal until there is lot of vertices.
1 parent cc01c3a commit 0bba382

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

src/hagl_polygon.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,15 @@ void hagl_fill_polygon(
115115
j = i;
116116
}
117117

118-
/* Sort the nodes, via a simple “Bubble” sort. */
119-
int16_t i = 0;
120-
while (i < count - 1) {
121-
if (nodes[i] > nodes[i + 1]) {
122-
int16_t swap = nodes[i];
123-
nodes[i] = nodes[i + 1];
124-
nodes[i + 1] = swap;
125-
if (i) {
126-
i--;
127-
}
128-
} else {
129-
i++;
118+
/* Sort the nodes, via insertion sort. */
119+
for (int16_t i = 1; i < count; i++) {
120+
int16_t key = nodes[i];
121+
int16_t j = i - 1;
122+
while (j >= 0 && nodes[j] > key) {
123+
nodes[j + 1] = nodes[j];
124+
j--;
130125
}
126+
nodes[j + 1] = key;
131127
}
132128

133129
/* Draw lines between nodes. */

0 commit comments

Comments
 (0)