diff -r 17a967c7a5e4b6c393ca025b5f0b416e070dc9f4 -r 5017e17bb2f1ffc93e7e81423c3d48c55c191570 cpp/algorithms/src/MedianSort.h --- a/cpp/algorithms/src/MedianSort.h Sat Feb 16 18:44:42 2013 -0600 +++ b/cpp/algorithms/src/MedianSort.h Sat Feb 16 22:28:24 2013 -0600 @@ -2,23 +2,46 @@ #define MEDIAN_SORT_H #include "SortAlgorithm.h" +#include #include template -class InsertSort : public SortAlgorithm +class MedianSort : public SortAlgorithm { public: virtual void Sort() { - this->mediansort(this->_container); + this->mediansort(0, this->_container.size()); } - void mediansort(std::vector arr) + void mediansort(size_t left, size_t right) { - if (arr.size() == 0) + throw exception("Does not work!"); + if (left >= right || (right - left) == 1) return; + //if (arr.size() == 0 || arr.size() == 1 || arr.size() == 2) + // return; + //int midpos = arr.size() / 2; + size_t pivot = right - left -1; + size_t mid = right / 2; + std::swap(this->_container[pivot], this->_container[mid]); + for(size_t i = left; i < mid - 1; i++) + { + if (this->_container.at(i) > this->_container.at(mid)) + { + for (size_t k = mid + 1; k < this->_container.size(); k++) + { + if (this->_container.at(k) <= this->_container.at(mid)) + { + std::swap(this->_container[i], this->_container[k]); + } + } + } + } + mediansort(left, left+mid-1); + mediansort(left+mid+1, right); } -} +}; #endif \ No newline at end of file diff -r 17a967c7a5e4b6c393ca025b5f0b416e070dc9f4 -r 5017e17bb2f1ffc93e7e81423c3d48c55c191570 cpp/algorithms/src/QuickSort.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/algorithms/src/QuickSort.h Sat Feb 16 22:28:24 2013 -0600 @@ -0,0 +1,39 @@ +#ifndef QUICKSORT_H +#define QUICKSORT_H + +#include "SortAlgorithm.h" +#include + +template +class QuickSort : public SortAlgorithm +{ +public: + virtual void Sort() + { + quicksort(0, this->_container.size() - 1); + } + + void quicksort(int left, int right) + { + if (right <= left) + return; + + int store; + int pivindex = left + right / 2; + std::swap(this->_container[pivindex], this->_container[right]); + store = left; + for(int i = left; i < right - 1; i++) + { + if (this->_container[i] <= this->_container[right]) + { + std::swap(this->_container[i], this->_container[store]); + store++; + } + } + std::swap(this->_container[store], this->_container[right]); + quicksort(left, store - 1); + quicksort(store + 1, right); + } +}; + +#endif \ No newline at end of file