From 68f362951d14d9099ffcde349b743b14c69cf049 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 22 Jan 2021 08:20:39 +0100 Subject: [PATCH] update insertSort and cleanup (#12) * fix insertSort + cleanup --- RunningMedian.cpp | 71 ++----------------- RunningMedian.h | 4 +- .../RunningMedian_large.ino | 16 ++--- library.json | 2 +- library.properties | 2 +- 5 files changed, 17 insertions(+), 78 deletions(-) diff --git a/RunningMedian.cpp b/RunningMedian.cpp index f5679e3..e21e0f4 100644 --- a/RunningMedian.cpp +++ b/RunningMedian.cpp @@ -1,7 +1,7 @@ // // FILE: RunningMedian.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.3.2 +// VERSION: 0.3.3 // PURPOSE: RunningMedian library for Arduino // // HISTORY: @@ -28,6 +28,7 @@ // 0.3.1 2021-01-16 Changed size parameter to 255 max // 0.3.2 2021-01-21 replaced bubbleSort by insertionSort // --> better performance for large arrays. +// 0.3.3 2021-01-22 better insertionSort (+ cleanup test code) #include "RunningMedian.h" @@ -190,77 +191,15 @@ void RunningMedian::sort() for (uint16_t i = 1; i < _count; i++) { uint16_t z = i; - while ((z > 0) && (_values[_sortIdx[z]] < _values[_sortIdx[z - 1]])) + uint16_t temp = _sortIdx[z]; + while ((z > 0) && (_values[temp] < _values[_sortIdx[z - 1]])) { - uint16_t temp = _sortIdx[z]; _sortIdx[z] = _sortIdx[z - 1]; - _sortIdx[z - 1] = temp; z--; } + _sortIdx[z] = temp; } _sorted = true; } - -///////////////////////////////////////////////////////////////////// -// -// Bubble sort - original -// -// void RunningMedian::sort() -// { - // // bubble sort with flag - worse for large values - // for (uint8_t i = 0; i < _count - 1; i++) - // { - // bool flag = true; - // for (uint8_t j = 1; j < _count - i; j++) - // { - // if (_values[_sortIdx[j - 1]] > _values[_sortIdx[j]]) - // { - // uint8_t t = _sortIdx[j - 1]; - // _sortIdx[j - 1] = _sortIdx[j]; - // _sortIdx[j] = t; - // flag = false; - // } - // } - // if (flag) break; - // } - // _sorted = true; -// } - - -///////////////////////////////////////////////////////////////////// -// -// Combsort - faster than bubble but slower than insertSort -// -// void RunningMedian::sort() -// { - // // COMBSORT - way faster than bubble sort but slower than insertSort - // uint16_t i, j; - // uint16_t gap; - // uint8_t swapped = 1; - // uint16_t temp; - - // gap = _count; - // while (gap > 1 || swapped == 1) - // { - // if (gap > 1) - // { - // gap = gap * 10/13; - // if (gap == 9 || gap == 10) gap = 11; - // } - // swapped = 0; - // for (i = 0, j = gap; j < _count; i++, j++) - // { - // if (_values[_sortIdx[j]] < _values[_sortIdx[i]]) - // { - // temp = _sortIdx[j]; - // _sortIdx[j] = _sortIdx[i]; - // _sortIdx[i] = temp; - // swapped = 1; - // } - // } - // } -// } - - // -- END OF FILE -- diff --git a/RunningMedian.h b/RunningMedian.h index cd55a76..32be4da 100644 --- a/RunningMedian.h +++ b/RunningMedian.h @@ -3,7 +3,7 @@ // FILE: RunningMedian.h // AUTHOR: Rob Tillaart // PURPOSE: RunningMedian library for Arduino -// VERSION: 0.3.2 +// VERSION: 0.3.3 // URL: https://github.com/RobTillaart/RunningMedian // URL: http://arduino.cc/playground/Main/RunningMedian // HISTORY: See RunningMedian.cpp @@ -12,7 +12,7 @@ #include "Arduino.h" -#define RUNNING_MEDIAN_VERSION (F("0.3.2")) +#define RUNNING_MEDIAN_VERSION (F("0.3.3")) // fall back to fixed storage for dynamic version => remove true diff --git a/examples/RunningMedian_large/RunningMedian_large.ino b/examples/RunningMedian_large/RunningMedian_large.ino index 995d58b..fe0ebe9 100644 --- a/examples/RunningMedian_large/RunningMedian_large.ino +++ b/examples/RunningMedian_large/RunningMedian_large.ino @@ -53,14 +53,14 @@ void loop() Serial.println(); } - // if (count == 255) - // { - // count++; - // for (int i = 0; i < 255; i++) - // { - // Serial.println(samples.getSortedElement(i)); - // } - // } + if (count == 255) + { + for (int i = 0; i < 255; i++) + { + Serial.println(samples.getSortedElement(i)); + } + } + count++; } // -- END OF FILE -- diff --git a/library.json b/library.json index 06d9f96..29fc786 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/RunningMedian.git" }, - "version":"0.3.2", + "version":"0.3.3", "frameworks": "arduino", "platforms": "*" } diff --git a/library.properties b/library.properties index 64a4dbc..c295e0b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=RunningMedian -version=0.3.2 +version=0.3.3 author=Rob Tillaart maintainer=Rob Tillaart sentence=The library stores the last N individual values in a buffer to select the median.