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:
23
04_quicksort/c++11/01_loop_sum.cpp
Normal file
23
04_quicksort/c++11/01_loop_sum.cpp
Normal 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;
|
||||
}
|
||||
22
04_quicksort/c++11/02_recursive_sum.cpp
Normal file
22
04_quicksort/c++11/02_recursive_sum.cpp
Normal 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;
|
||||
}
|
||||
17
04_quicksort/c++11/03_recursive_count.cpp
Normal file
17
04_quicksort/c++11/03_recursive_count.cpp
Normal 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;
|
||||
}
|
||||
27
04_quicksort/c++11/04_recursive_max.cpp
Normal file
27
04_quicksort/c++11/04_recursive_max.cpp
Normal 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;
|
||||
}
|
||||
40
04_quicksort/c++11/05_quicksort.cpp
Normal file
40
04_quicksort/c++11/05_quicksort.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user