commonlib: Add Bubble sort algorithm

Add an implementation for Bubble sort. For now, only integers can be
sorted in an ascending or descending order. It can be later simply
extended to cover other datasets like strings if needed.

The reasons for choosing bubble sort are:
* it is a simple algorithm
* bubble sort is stable, i.e. it does not exchange entries which are not
  needed to be sorted as they are already in order

Change-Id: I2c5e0b5685a907243b58ebe6682078272d316bf6
Signed-off-by: Werner Zeh <werner.zeh@siemens.com>
Reviewed-on: https://review.coreboot.org/c/31544
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Werner Zeh 2019-02-19 13:34:12 +01:00 committed by Patrick Georgi
parent 38b6ccfed9
commit bd660e2338
4 changed files with 83 additions and 0 deletions

View File

@ -36,3 +36,5 @@ verstage-y += lz4_wrapper.c
romstage-y += lz4_wrapper.c romstage-y += lz4_wrapper.c
ramstage-y += lz4_wrapper.c ramstage-y += lz4_wrapper.c
postcar-y += lz4_wrapper.c postcar-y += lz4_wrapper.c
ramstage-y += sort.c

View File

@ -38,6 +38,11 @@
#define ABS(a) (((a) < 0) ? (-(a)) : (a)) #define ABS(a) (((a) < 0) ? (-(a)) : (a))
#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0) #define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
#define DIV_ROUND_UP(x, y) (((x) + (y) - 1) / (y)) #define DIV_ROUND_UP(x, y) (((x) + (y) - 1) / (y))
#define SWAP(a, b) do { \
typeof(a) tmp = a; \
a = (typeof(a)) b; \
b = (typeof(b)) tmp; \
} while (0)
/* /*
* Divide positive or negative dividend by positive divisor and round * Divide positive or negative dividend by positive divisor and round
* to closest integer. Result is undefined for negative divisors and * to closest integer. Result is undefined for negative divisors and

View File

@ -0,0 +1,27 @@
/*
* This file is part of the coreboot project.
*
* Copyright (C) 2019 Siemens AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _COMMONLIB_SORT_H_
#define _COMMONLIB_SORT_H_
#include <stddef.h>
typedef enum {
NUM_ASCENDING,
NUM_DESCENDING
} sort_order_t;
void bubblesort(int *v, size_t num_entries, sort_order_t order);
#endif /* _COMMONLIB_SORT_H_ */

49
src/commonlib/sort.c Normal file
View File

@ -0,0 +1,49 @@
/*
* This file is part of the coreboot project.
*
* Copyright (C) 2019 Siemens AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <commonlib/helpers.h>
#include <commonlib/sort.h>
/* Implement a simple Bubble sort algorithm. Reduce the needed number of
iterations by taking care of already sorted entries in the list. */
void bubblesort(int *v, size_t num_entries, sort_order_t order)
{
size_t i, j;
int swapped;
for (j = 0; j < num_entries - 1; j++) {
swapped = 0;
for (i = 0; i < num_entries - j - 1; i++) {
switch (order) {
case NUM_ASCENDING:
if (v[i] > v[i + 1]) {
SWAP(v[i], v[i + 1]);
swapped = 1;
}
break;
case NUM_DESCENDING:
if (v[i] < v[i + 1]) {
SWAP(v[i], v[i + 1]);
swapped = 1;
}
break;
default:
return;
}
}
if (!swapped)
break;
}
}