Documentation: Get rid of trailing whitespaces
[coreboot.git] / src / commonlib / sort.c
blob6fd1a40549122a06e88344de13068be53b86b4a1
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/helpers.h>
4 #include <commonlib/sort.h>
6 /* Implement a simple Bubble sort algorithm. Reduce the needed number of
7 iterations by taking care of already sorted entries in the list. */
8 void bubblesort(int *v, size_t num_entries, sort_order_t order)
10 size_t i, j;
11 int swapped;
13 /* Make sure there are at least two entries to sort. */
14 if (num_entries < 2)
15 return;
17 for (j = 0; j < num_entries - 1; j++) {
18 swapped = 0;
19 for (i = 0; i < num_entries - j - 1; i++) {
20 switch (order) {
21 case NUM_ASCENDING:
22 if (v[i] > v[i + 1]) {
23 SWAP(v[i], v[i + 1]);
24 swapped = 1;
26 break;
27 case NUM_DESCENDING:
28 if (v[i] < v[i + 1]) {
29 SWAP(v[i], v[i + 1]);
30 swapped = 1;
32 break;
33 default:
34 return;
37 if (!swapped)
38 break;