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,67 @@
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
int binary_search(const std::vector<T>& list, const int& item) {
int low = 0;
int high = list.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
T guess = list[mid];
if (guess == item) {
return mid;
}
if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
// this function returns pointer to the found element rather than array index
template <typename T>
const T* binary_search2(const std::vector<T>& list, const T& item) {
const T* low = &list.front();
const T* high = &list.back();
while (low <= high) {
// "guess" is the element in the middle between "high" and "low"
const T* guess = low + ((high - low) / 2);
if (*guess == item)
return guess;
if (*guess > item) {
high = guess - 1;
} else {
low = guess + 1;
}
}
return nullptr;
}
int main() {
std::vector<int> my_list = {1, 3, 5, 7, 9};
const int* binary_search2_result = binary_search2(my_list, 9);
const int* binary_search2_null = binary_search2(my_list, 4); // test finding element that is not in the list
cout << "Binary search for number 3: " << binary_search(my_list, 3) << endl;
cout << "Binary search 2 for number 9 (memory address): " << binary_search2_result << endl;
cout << "Binary search 2 for number 9 (value): " << *binary_search2_result << endl;
if (binary_search2_null == nullptr) {
cout << "4 was not found in the list" << endl;
}
return 0;
}

View File

@@ -0,0 +1,10 @@
CPP=g++
CPPFLAGS=-Wall -std=c++11
MAIN_NAME=main
objects=01_binary_search.o
main: $(objects)
$(CPP) -std=c++11 $(CPPFLAGS) -o $(MAIN_NAME) $(objects)
clean:
rm -f $(MAIN_NAME) $(objects)

View File

@@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
// Finds the smallest value in an array
template <typename T>
int find_smallest(const std::vector<T>& arr) {
// stores smallest value
T smallest = arr[0];
// stores index of the smallest value
int smallest_index = 0;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] < smallest) {
smallest = arr[i];
smallest_index = i;
}
}
return smallest_index;
}
template <typename T>
std::vector<T> selection_sort(std::vector<T> arr) {
std::vector<T> sorted;
while(!arr.empty()) {
// find smallest element and add it to sorted array
int smallest_index = find_smallest(arr);
sorted.push_back(arr[smallest_index]);
// remove smallest element from non-sorted array
arr.erase(arr.begin() + smallest_index);
}
return sorted;
}
int main() {
std::vector<float> arr = {1.2, 1.0, 3, 0, -1, 0.5, 100, -99};
std::vector<float> sorted = selection_sort(arr);
cout << "Sorted array: ";
for (float num : sorted) {
cout << num << " ";
}
cout << endl;
}

View File

@@ -0,0 +1,10 @@
CPP=g++
CPPFLAGS=-Wall -std=c++11
MAIN_NAME=main
objects=01_selection_sort.o
main: $(objects)
$(CPP) -std=c++11 $(CPPFLAGS) -o $(MAIN_NAME) $(objects)
clean:
rm -f $(MAIN_NAME) $(objects)

View File

@@ -0,0 +1,18 @@
#include <iostream>
using std::cout;
using std::endl;
void countdown(const int& i) {
cout << i << endl;
// base case
if (i <= 0) return;\
// recursive case
countdown(i - 1);
}
int main() {
countdown(5);
}

View File

@@ -0,0 +1,27 @@
#include <iostream>
#include <string>
using std::cout;
using std::endl;
void greet2(std::string name) {
cout << "How are you, " + name + "?" << endl;
}
void bye() {
cout << "Ok, bye!" << endl;
}
void greet(std::string name) {
cout << "Hello, " + name + "!" << endl;
greet2(name);
cout << "Getting ready to say bye..." << endl;
}
int main() {
greet("Adit");
return 0;
}

View File

@@ -0,0 +1,13 @@
#include <iostream>
using std::cout;
using std::endl;
int fact(const int& x) {
if (x == 1) return 1;
return fact(x-1) * x;
}
int main() {
cout << fact(5) << endl;
}

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;
}

View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <unordered_map>
#include <string>
#include <utility>
using std::cout;
using std::endl;
int main() {
std::unordered_map<std::string, float> book = {
{"apple", 0.67},
{"milk", 1.49},
{"avocado", 1.49}
};
// print book
for (std::pair<std::string, float> pair : book) {
cout << pair.first << ": " << pair.second << "$" << endl;
}
}

View File

@@ -0,0 +1,24 @@
#include <iostream>
#include <unordered_map>
#include <string>
using std::cout;
using std::endl;
std::unordered_map<std::string, bool> voted;
void check_voter(const std::string& name) {
auto search = voted.find(name);
if (search == voted.end() || search->second == false) {
voted.insert({name, true});
cout << "Let them vote!" << endl;;
} else {
cout << "Kick them out!" << endl;
}
}
int main() {
check_voter("tom");
check_voter("mike");
check_voter("mike");
}

View File

@@ -0,0 +1,64 @@
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <queue>
#include <unordered_set>
using std::cout;
using std::endl;
bool is_seller(const std::string& name) {
return name.back() == 'm';
}
template <typename T>
bool search(const T& name, const std::unordered_map<T, std::vector<T>>& graph) {
std::queue<T> search_queue;
std::unordered_set<T> searched;
// add all friends to search queue
for (auto friend_name : graph.find(name) -> second) {
search_queue.push(friend_name);
}
while (!search_queue.empty()) {
T& person = search_queue.front();
search_queue.pop();
// only search this person if you haven't already searched them.
if (searched.find(person) == searched.end()) {
if (is_seller(person)) {
cout << person << " is a mango seller!" << endl;
return true;
}
std::vector<T> friend_list = graph.find(person) -> second;
// add all friends of a person to search queue
for (T friend_name : friend_list) {
search_queue.push(friend_name);
}
// mark this person as searched
searched.insert(person);
}
}
return false;
}
int main() {
std::unordered_map<std::string, std::vector<std::string>> graph;
graph.insert({"you", {"alice", "bob", "claire"}});
graph.insert({"bob", {"anuj", "peggy"}});
graph.insert({"alice", {"peggy"}});
graph.insert({"claire", {"thom", "jonny"}});
graph.insert({"anuj", {}});
graph.insert({"peggy", {}});
graph.insert({"thom", {}});
graph.insert({"jonny", {}});
std::string name = "you";
bool result = search(name, graph);
cout << "Found mango seller: " << result << endl;
}