diff options
Diffstat (limited to 'algo1w1/IntOrderedArray.cpp')
-rw-r--r-- | algo1w1/IntOrderedArray.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/algo1w1/IntOrderedArray.cpp b/algo1w1/IntOrderedArray.cpp new file mode 100644 index 0000000..d84401f --- /dev/null +++ b/algo1w1/IntOrderedArray.cpp @@ -0,0 +1,83 @@ +#include "IntOrderedArray.h" +#include <iostream> + +IntOrderedArray::IntOrderedArray() { +} + + +IntOrderedArray::~IntOrderedArray() { +} + +void IntOrderedArray::exploreBinarySearch(int searchKey) const { + int lowerBound = 0; + int upperBound = _collectionSize - 1; + int curIn; + while(1) { + curIn = (lowerBound + upperBound ) / 2; + if(_collection[curIn] == searchKey) + return (void) curIn; // found it + else if(lowerBound > upperBound) { + std::cout << "[" << lowerBound << ", " << upperBound << "]" << std::endl; + return (void) _collectionSize; // can’t find it + } + else // divide range + { + if(_collection[curIn] < searchKey) + lowerBound = curIn + 1; // it’s in upper half + else + upperBound = curIn - 1; // it’s in lower half + } // end find() + } +} + +/* + * vul hieronder de tabel in voor vraag 3.b + * + * waarde lowerbound upprbound + * ~~~~~~~~~ + * 0 [0, -1] + * 2 [1, 0] + * 4 [2, 1] + * 5 [2, 1] + * 23 [6, 5] + * 26 [6, 5] + * 30 [7, 6] + */ + +/* + * formuleer hieronder het antwoord op vraag 3.c + * + * in een ideale wereld waar alle array's alle elementen bevatten die je maar + * zou willen zoeken zou de index van het nummer dat je zoekt tussen upperBound + * en lowerBound liggen, e.g. "4" zou tussen 3 en 6 liggen met index 1 en 2. + */ + +int IntOrderedArray::getLastElementSmallerOrEqualTo(int) const { + return -1; +} + +void IntOrderedArray::moveElementsOnePositionToRight(int) { + // deze code wordt nergens gebruikt(?) +} + +int IntOrderedArray::quickInsert(int value) { + int i; + // find insert index + for (i = 0; i < _collectionSize; i++) { + if (_collection[i] > value) break; + } + // shift elements + for (int k = _collectionSize; k >= i; k--) { + if (k == 0) continue; + _collection[k] = _collection[k-1]; + } + _collectionSize++; + _collection[i] = value; + return i; +} + +void IntOrderedArray::showAll() const { + for (unsigned i = 0; i < _collectionSize; i++) { + printf("%02d: %d\n", i, _collection[i]); + } +} |