add c++11 (#87)

* binary search c++

* selection sort c++11

* c++ recursive countdown

* c++ recursion

* c++ quicksort

* rename folder names to c++11

* add another version of binary_search function

* c++11 hash tables

* c++11 breadth-first search
This commit is contained in:
umatbro
2018-10-18 17:25:54 +02:00
committed by Aditya Bhargava
parent 38f5b2792e
commit 7dc9e95d2a
15 changed files with 432 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
T sum(const std::vector<T>& arr) {
T sum = 0;
for (T item : arr) {
sum += item;
}
return sum;
}
int main() {
std::vector<int> arr_int = {1, 2, 3, 4};
std::vector<float> arr_float = {0.1, 0.2, 0.3, 0.4, 0.5};
cout << "Sum ints: " << sum(arr_int) << endl;
cout << "Sum floats: " << sum(arr_float) << endl;
}

View File

@@ -0,0 +1,22 @@
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
T sum(std::vector<T> arr) {
if (arr.empty()) return 0;
T last_num = arr.back(); // save last number value
arr.pop_back(); // and remove it from array for next recursive call
return first_num + sum(arr);
}
int main() {
std::vector<int> arr_int = {1, 2, 3, 4};
std::vector<float> arr_float = {0.1, 0.2, 0.3, 0.4, 0.5};
cout << "Sum ints: " << sum(arr_int) << endl;
cout << "Sum floats: " << sum(arr_float) << endl;
}

View File

@@ -0,0 +1,17 @@
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
int count(std::vector<T> arr) {
if (arr.empty()) return 0;
arr.pop_back();
return count(arr) + 1;
}
int main() {
std::vector<int> array = {0, 1, 2, 3, 4, 5};
cout << count(array) << endl;
}

View File

@@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <stdexcept>
using std::cout;
using std::endl;
template <typename T>
T max(std::vector<T> arr) {
if (arr.empty()) throw std::invalid_argument("Cannot select max value from empty sequence");
if (arr.size() == 1) return arr.at(0);
T back = arr.back();
arr.pop_back();
T sub_max = max(arr);
return back > sub_max ? back : sub_max;
}
int main() {
std::vector<int> array = {1, 5, 10, 25, 16, 1};
cout << max(array) << endl;
std::vector<int> negative_array = {-1, -5, -10, -25, -16};
cout << max(negative_array) << endl;
}

View File

@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
std::vector<T> quicksort(const std::vector<T>& arr) {
// base case, arrays with 0 or 1 element are already "sorted"
if (arr.size() < 2)
return arr;
// recursive case
const T* pivot = &arr.front() + arr.size() / 2 - 1; // set the pivot somewhere in the middle
std::vector<T> less; // vector to store all the elements less than the pivot
std::vector<T> greater; // vector to store all the elements greater than the pivot
for (const T* item = &arr.front(); item <= &arr.back(); item++) {
if (item == pivot) continue; // skip pivot element
if (*item <= *pivot) less.push_back(*item);
else greater.push_back(*item);
}
std::vector<T> sorted_less = quicksort(less);
std::vector<T> sorted_greater = quicksort(greater);
// concatenate less part, pivot and greater part
sorted_less.push_back(*pivot);
sorted_less.insert(sorted_less.end(), sorted_greater.begin(), sorted_greater.end());
return sorted_less;
}
int main() {
std::vector<int> arr = {69, 60, 38, 82, 99, 15, 8, 94, 30, 42, 35, 40, 63, 1, 49, 66, 93, 83, 20, 32, 87, 6, 78, 17, 2, 61, 91, 25, 7, 4, 97, 31, 23, 67, 95, 47, 55, 92, 37, 59, 73, 81, 74, 41, 39};
std::vector<int> sorted = quicksort(arr);
for (int num : sorted) {
cout << num << " ";
}
cout << endl;
}