From 7dc9e95d2a9e505c89d9f0649f58daf34ee9e9d6 Mon Sep 17 00:00:00 2001 From: umatbro Date: Thu, 18 Oct 2018 17:25:54 +0200 Subject: [PATCH] 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 --- .../c++11/01_binary_search.cpp | 67 +++++++++++++++++++ 01_introduction_to_algorithms/c++11/Makefile | 10 +++ 02_selection_sort/c++11/01_selection_sort.cpp | 50 ++++++++++++++ 02_selection_sort/c++11/Makefile | 10 +++ 03_recursion/c++11/01_countdown.cpp | 18 +++++ 03_recursion/c++11/02_greet.cpp | 27 ++++++++ 03_recursion/c++11/03_factorial.cpp | 13 ++++ 04_quicksort/c++11/01_loop_sum.cpp | 23 +++++++ 04_quicksort/c++11/02_recursive_sum.cpp | 22 ++++++ 04_quicksort/c++11/03_recursive_count.cpp | 17 +++++ 04_quicksort/c++11/04_recursive_max.cpp | 27 ++++++++ 04_quicksort/c++11/05_quicksort.cpp | 40 +++++++++++ .../c++11/01_price_of_groceries.cpp | 20 ++++++ 05_hash_tables/c++11/02_check_voter.cpp | 24 +++++++ .../c++11/01_breadth-first_search.cpp | 64 ++++++++++++++++++ 15 files changed, 432 insertions(+) create mode 100644 01_introduction_to_algorithms/c++11/01_binary_search.cpp create mode 100644 01_introduction_to_algorithms/c++11/Makefile create mode 100644 02_selection_sort/c++11/01_selection_sort.cpp create mode 100644 02_selection_sort/c++11/Makefile create mode 100644 03_recursion/c++11/01_countdown.cpp create mode 100644 03_recursion/c++11/02_greet.cpp create mode 100644 03_recursion/c++11/03_factorial.cpp create mode 100644 04_quicksort/c++11/01_loop_sum.cpp create mode 100644 04_quicksort/c++11/02_recursive_sum.cpp create mode 100644 04_quicksort/c++11/03_recursive_count.cpp create mode 100644 04_quicksort/c++11/04_recursive_max.cpp create mode 100644 04_quicksort/c++11/05_quicksort.cpp create mode 100644 05_hash_tables/c++11/01_price_of_groceries.cpp create mode 100644 05_hash_tables/c++11/02_check_voter.cpp create mode 100644 06_breadth-first_search/c++11/01_breadth-first_search.cpp diff --git a/01_introduction_to_algorithms/c++11/01_binary_search.cpp b/01_introduction_to_algorithms/c++11/01_binary_search.cpp new file mode 100644 index 0000000..f0e06e5 --- /dev/null +++ b/01_introduction_to_algorithms/c++11/01_binary_search.cpp @@ -0,0 +1,67 @@ +#include +#include + +using std::cout; +using std::endl; + +template +int binary_search(const std::vector& 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 +const T* binary_search2(const std::vector& 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 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; +} diff --git a/01_introduction_to_algorithms/c++11/Makefile b/01_introduction_to_algorithms/c++11/Makefile new file mode 100644 index 0000000..348f47d --- /dev/null +++ b/01_introduction_to_algorithms/c++11/Makefile @@ -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) diff --git a/02_selection_sort/c++11/01_selection_sort.cpp b/02_selection_sort/c++11/01_selection_sort.cpp new file mode 100644 index 0000000..ba89b27 --- /dev/null +++ b/02_selection_sort/c++11/01_selection_sort.cpp @@ -0,0 +1,50 @@ +#include +#include + +using std::cout; +using std::endl; + +// Finds the smallest value in an array +template +int find_smallest(const std::vector& 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 +std::vector selection_sort(std::vector arr) { + std::vector 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 arr = {1.2, 1.0, 3, 0, -1, 0.5, 100, -99}; + std::vector sorted = selection_sort(arr); + + cout << "Sorted array: "; + for (float num : sorted) { + cout << num << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/02_selection_sort/c++11/Makefile b/02_selection_sort/c++11/Makefile new file mode 100644 index 0000000..a899f36 --- /dev/null +++ b/02_selection_sort/c++11/Makefile @@ -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) diff --git a/03_recursion/c++11/01_countdown.cpp b/03_recursion/c++11/01_countdown.cpp new file mode 100644 index 0000000..681cb54 --- /dev/null +++ b/03_recursion/c++11/01_countdown.cpp @@ -0,0 +1,18 @@ +#include + +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); +} diff --git a/03_recursion/c++11/02_greet.cpp b/03_recursion/c++11/02_greet.cpp new file mode 100644 index 0000000..c743a89 --- /dev/null +++ b/03_recursion/c++11/02_greet.cpp @@ -0,0 +1,27 @@ +#include +#include + +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; +} \ No newline at end of file diff --git a/03_recursion/c++11/03_factorial.cpp b/03_recursion/c++11/03_factorial.cpp new file mode 100644 index 0000000..3119f19 --- /dev/null +++ b/03_recursion/c++11/03_factorial.cpp @@ -0,0 +1,13 @@ +#include + +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; +} diff --git a/04_quicksort/c++11/01_loop_sum.cpp b/04_quicksort/c++11/01_loop_sum.cpp new file mode 100644 index 0000000..6417abe --- /dev/null +++ b/04_quicksort/c++11/01_loop_sum.cpp @@ -0,0 +1,23 @@ +#include +#include + +using std::cout; +using std::endl; + +template +T sum(const std::vector& arr) { + T sum = 0; + for (T item : arr) { + sum += item; + } + + return sum; +} + +int main() { + std::vector arr_int = {1, 2, 3, 4}; + std::vector 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; +} diff --git a/04_quicksort/c++11/02_recursive_sum.cpp b/04_quicksort/c++11/02_recursive_sum.cpp new file mode 100644 index 0000000..b75a820 --- /dev/null +++ b/04_quicksort/c++11/02_recursive_sum.cpp @@ -0,0 +1,22 @@ +#include +#include + +using std::cout; +using std::endl; + +template +T sum(std::vector 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 arr_int = {1, 2, 3, 4}; + std::vector 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; +} diff --git a/04_quicksort/c++11/03_recursive_count.cpp b/04_quicksort/c++11/03_recursive_count.cpp new file mode 100644 index 0000000..333ee69 --- /dev/null +++ b/04_quicksort/c++11/03_recursive_count.cpp @@ -0,0 +1,17 @@ +#include +#include + +using std::cout; +using std::endl; + +template +int count(std::vector arr) { + if (arr.empty()) return 0; + arr.pop_back(); + return count(arr) + 1; +} + +int main() { + std::vector array = {0, 1, 2, 3, 4, 5}; + cout << count(array) << endl; +} diff --git a/04_quicksort/c++11/04_recursive_max.cpp b/04_quicksort/c++11/04_recursive_max.cpp new file mode 100644 index 0000000..1d110dc --- /dev/null +++ b/04_quicksort/c++11/04_recursive_max.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using std::cout; +using std::endl; + +template +T max(std::vector 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 array = {1, 5, 10, 25, 16, 1}; + cout << max(array) << endl; + + std::vector negative_array = {-1, -5, -10, -25, -16}; + cout << max(negative_array) << endl; +} diff --git a/04_quicksort/c++11/05_quicksort.cpp b/04_quicksort/c++11/05_quicksort.cpp new file mode 100644 index 0000000..b0c5744 --- /dev/null +++ b/04_quicksort/c++11/05_quicksort.cpp @@ -0,0 +1,40 @@ +#include +#include + +using std::cout; +using std::endl; + +template +std::vector quicksort(const std::vector& 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 less; // vector to store all the elements less than the pivot + std::vector 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 sorted_less = quicksort(less); + std::vector 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 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 sorted = quicksort(arr); + for (int num : sorted) { + cout << num << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/05_hash_tables/c++11/01_price_of_groceries.cpp b/05_hash_tables/c++11/01_price_of_groceries.cpp new file mode 100644 index 0000000..7f6a623 --- /dev/null +++ b/05_hash_tables/c++11/01_price_of_groceries.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +using std::cout; +using std::endl; + +int main() { + std::unordered_map book = { + {"apple", 0.67}, + {"milk", 1.49}, + {"avocado", 1.49} + }; + + // print book + for (std::pair pair : book) { + cout << pair.first << ": " << pair.second << "$" << endl; + } +} \ No newline at end of file diff --git a/05_hash_tables/c++11/02_check_voter.cpp b/05_hash_tables/c++11/02_check_voter.cpp new file mode 100644 index 0000000..aa27866 --- /dev/null +++ b/05_hash_tables/c++11/02_check_voter.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +using std::cout; +using std::endl; + +std::unordered_map 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"); +} \ No newline at end of file diff --git a/06_breadth-first_search/c++11/01_breadth-first_search.cpp b/06_breadth-first_search/c++11/01_breadth-first_search.cpp new file mode 100644 index 0000000..2053590 --- /dev/null +++ b/06_breadth-first_search/c++11/01_breadth-first_search.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include + +using std::cout; +using std::endl; + +bool is_seller(const std::string& name) { + return name.back() == 'm'; +} + +template +bool search(const T& name, const std::unordered_map>& graph) { + std::queue search_queue; + std::unordered_set 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 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> 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; +} \ No newline at end of file