Skip to content

Commit

Permalink
update insertSort and cleanup (#12)
Browse files Browse the repository at this point in the history
* fix insertSort + cleanup
  • Loading branch information
RobTillaart authored Jan 22, 2021
1 parent c3de2e1 commit 68f3629
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 78 deletions.
71 changes: 5 additions & 66 deletions RunningMedian.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: RunningMedian.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.2
// VERSION: 0.3.3
// PURPOSE: RunningMedian library for Arduino
//
// HISTORY:
Expand All @@ -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"
Expand Down Expand Up @@ -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 --
4 changes: 2 additions & 2 deletions RunningMedian.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions examples/RunningMedian_large/RunningMedian_large.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/RunningMedian.git"
},
"version":"0.3.2",
"version":"0.3.3",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=RunningMedian
version=0.3.2
version=0.3.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=The library stores the last N individual values in a buffer to select the median.
Expand Down

0 comments on commit 68f3629

Please sign in to comment.