From 540f94c234dd169dfe37a665e8a6c01f67b75d21 Mon Sep 17 00:00:00 2001 From: umatbro Date: Tue, 28 Aug 2018 16:25:54 +0200 Subject: [PATCH 1/9] binary search c++ --- .../c++/01_binary_search.cpp | 37 +++++++++++++++++++ 01_introduction_to_algorithms/c++/Makefile | 10 +++++ 2 files changed, 47 insertions(+) create mode 100644 01_introduction_to_algorithms/c++/01_binary_search.cpp create mode 100644 01_introduction_to_algorithms/c++/Makefile diff --git a/01_introduction_to_algorithms/c++/01_binary_search.cpp b/01_introduction_to_algorithms/c++/01_binary_search.cpp new file mode 100644 index 00000000..7e26efe5 --- /dev/null +++ b/01_introduction_to_algorithms/c++/01_binary_search.cpp @@ -0,0 +1,37 @@ +#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; +} + + +int main() { + std::vector my_list = {1, 3, 5, 7, 9}; + cout << "Binary search for number 3: " << binary_search(my_list, 3) << endl; + cout << "Binary search for number 4: " << binary_search(my_list, 4) << endl; + + return 0; +} diff --git a/01_introduction_to_algorithms/c++/Makefile b/01_introduction_to_algorithms/c++/Makefile new file mode 100644 index 00000000..348f47d0 --- /dev/null +++ b/01_introduction_to_algorithms/c++/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) From cfde60aa6d2b6ff2db3e6e6cfc8c42d3c4704578 Mon Sep 17 00:00:00 2001 From: umatbro Date: Wed, 29 Aug 2018 15:27:00 +0200 Subject: [PATCH 2/9] selection sort c++11 --- 02_selection_sort/c++/01_selection_sort.cpp | 50 +++++++++++++++++++++ 02_selection_sort/c++/Makefile | 10 +++++ 2 files changed, 60 insertions(+) create mode 100644 02_selection_sort/c++/01_selection_sort.cpp create mode 100644 02_selection_sort/c++/Makefile diff --git a/02_selection_sort/c++/01_selection_sort.cpp b/02_selection_sort/c++/01_selection_sort.cpp new file mode 100644 index 00000000..ba89b278 --- /dev/null +++ b/02_selection_sort/c++/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++/Makefile b/02_selection_sort/c++/Makefile new file mode 100644 index 00000000..a899f366 --- /dev/null +++ b/02_selection_sort/c++/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) From 6f68339eeb35433d9b7f7f32b3134f422aca48f7 Mon Sep 17 00:00:00 2001 From: umatbro Date: Wed, 29 Aug 2018 23:22:22 +0200 Subject: [PATCH 3/9] c++ recursive countdown --- 03_recursion/c++/01_countdown.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 03_recursion/c++/01_countdown.cpp diff --git a/03_recursion/c++/01_countdown.cpp b/03_recursion/c++/01_countdown.cpp new file mode 100644 index 00000000..07168770 --- /dev/null +++ b/03_recursion/c++/01_countdown.cpp @@ -0,0 +1,17 @@ +#include + +using std::cout; +using std::endl; + +void countdown(const int& i) { + cout << i << endl; + + if (i <= 0) { + return; + } + countdown(i - 1); +} + +int main() { + countdown(5); +} From 01dc9e3941ee6dae3aba2fe5602fbc2267bdc93b Mon Sep 17 00:00:00 2001 From: umatbro Date: Thu, 30 Aug 2018 14:16:47 +0200 Subject: [PATCH 4/9] c++ recursion --- 03_recursion/c++/01_countdown.cpp | 35 ++++++++++++++++--------------- 03_recursion/c++/02_greet.cpp | 27 ++++++++++++++++++++++++ 03_recursion/c++/03_factorial.cpp | 13 ++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 03_recursion/c++/02_greet.cpp create mode 100644 03_recursion/c++/03_factorial.cpp diff --git a/03_recursion/c++/01_countdown.cpp b/03_recursion/c++/01_countdown.cpp index 07168770..681cb545 100644 --- a/03_recursion/c++/01_countdown.cpp +++ b/03_recursion/c++/01_countdown.cpp @@ -1,17 +1,18 @@ -#include - -using std::cout; -using std::endl; - -void countdown(const int& i) { - cout << i << endl; - - if (i <= 0) { - return; - } - countdown(i - 1); -} - -int main() { - countdown(5); -} +#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++/02_greet.cpp b/03_recursion/c++/02_greet.cpp new file mode 100644 index 00000000..c743a898 --- /dev/null +++ b/03_recursion/c++/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++/03_factorial.cpp b/03_recursion/c++/03_factorial.cpp new file mode 100644 index 00000000..3119f19a --- /dev/null +++ b/03_recursion/c++/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; +} From 772547f7fd8a429eca06f05d86b6ad14cbba0c5b Mon Sep 17 00:00:00 2001 From: umatbro Date: Thu, 30 Aug 2018 16:16:25 +0200 Subject: [PATCH 5/9] c++ quicksort --- 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 +++++++++++++++++++++++ 5 files changed, 129 insertions(+) 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 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 00000000..6417abe1 --- /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 00000000..b75a8201 --- /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 00000000..333ee69e --- /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 00000000..1d110dcb --- /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 00000000..b0c57448 --- /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 From a253980555a1e5ca6c586860dde8db6320252662 Mon Sep 17 00:00:00 2001 From: umatbro Date: Sat, 1 Sep 2018 00:24:49 +0200 Subject: [PATCH 6/9] rename folder names to c++11 --- 01_introduction_to_algorithms/{c++ => c++11}/01_binary_search.cpp | 0 01_introduction_to_algorithms/{c++ => c++11}/Makefile | 0 02_selection_sort/{c++ => c++11}/01_selection_sort.cpp | 0 02_selection_sort/{c++ => c++11}/Makefile | 0 03_recursion/{c++ => c++11}/01_countdown.cpp | 0 03_recursion/{c++ => c++11}/02_greet.cpp | 0 03_recursion/{c++ => c++11}/03_factorial.cpp | 0 7 files changed, 0 insertions(+), 0 deletions(-) rename 01_introduction_to_algorithms/{c++ => c++11}/01_binary_search.cpp (100%) rename 01_introduction_to_algorithms/{c++ => c++11}/Makefile (100%) rename 02_selection_sort/{c++ => c++11}/01_selection_sort.cpp (100%) rename 02_selection_sort/{c++ => c++11}/Makefile (100%) rename 03_recursion/{c++ => c++11}/01_countdown.cpp (100%) rename 03_recursion/{c++ => c++11}/02_greet.cpp (100%) rename 03_recursion/{c++ => c++11}/03_factorial.cpp (100%) diff --git a/01_introduction_to_algorithms/c++/01_binary_search.cpp b/01_introduction_to_algorithms/c++11/01_binary_search.cpp similarity index 100% rename from 01_introduction_to_algorithms/c++/01_binary_search.cpp rename to 01_introduction_to_algorithms/c++11/01_binary_search.cpp diff --git a/01_introduction_to_algorithms/c++/Makefile b/01_introduction_to_algorithms/c++11/Makefile similarity index 100% rename from 01_introduction_to_algorithms/c++/Makefile rename to 01_introduction_to_algorithms/c++11/Makefile diff --git a/02_selection_sort/c++/01_selection_sort.cpp b/02_selection_sort/c++11/01_selection_sort.cpp similarity index 100% rename from 02_selection_sort/c++/01_selection_sort.cpp rename to 02_selection_sort/c++11/01_selection_sort.cpp diff --git a/02_selection_sort/c++/Makefile b/02_selection_sort/c++11/Makefile similarity index 100% rename from 02_selection_sort/c++/Makefile rename to 02_selection_sort/c++11/Makefile diff --git a/03_recursion/c++/01_countdown.cpp b/03_recursion/c++11/01_countdown.cpp similarity index 100% rename from 03_recursion/c++/01_countdown.cpp rename to 03_recursion/c++11/01_countdown.cpp diff --git a/03_recursion/c++/02_greet.cpp b/03_recursion/c++11/02_greet.cpp similarity index 100% rename from 03_recursion/c++/02_greet.cpp rename to 03_recursion/c++11/02_greet.cpp diff --git a/03_recursion/c++/03_factorial.cpp b/03_recursion/c++11/03_factorial.cpp similarity index 100% rename from 03_recursion/c++/03_factorial.cpp rename to 03_recursion/c++11/03_factorial.cpp From 09cc051d40a5f0b398b044252c8b80d7f73b3fe5 Mon Sep 17 00:00:00 2001 From: umatbro Date: Sat, 1 Sep 2018 12:40:08 +0200 Subject: [PATCH 7/9] add another version of binary_search function --- .../c++11/01_binary_search.cpp | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/01_introduction_to_algorithms/c++11/01_binary_search.cpp b/01_introduction_to_algorithms/c++11/01_binary_search.cpp index 7e26efe5..f0e06e51 100644 --- a/01_introduction_to_algorithms/c++11/01_binary_search.cpp +++ b/01_introduction_to_algorithms/c++11/01_binary_search.cpp @@ -4,7 +4,7 @@ using std::cout; using std::endl; -template +template int binary_search(const std::vector& list, const int& item) { int low = 0; int high = list.size() - 1; @@ -27,11 +27,41 @@ int binary_search(const std::vector& list, const int& item) { 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 for number 4: " << binary_search(my_list, 4) << 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; } From ba5f13a0ddb49c6f5b45b1041c17940198167471 Mon Sep 17 00:00:00 2001 From: umatbro Date: Sat, 8 Sep 2018 22:37:29 +0200 Subject: [PATCH 8/9] c++11 hash tables --- .../c++11/01_price_of_groceries.cpp | 20 ++++++++++++++++ 05_hash_tables/c++11/02_check_voter.cpp | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+) 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 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 00000000..7f6a6233 --- /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 00000000..aa278660 --- /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 From daa66e4876f3ae2f6e348cfd3d2c48e818535936 Mon Sep 17 00:00:00 2001 From: umatbro Date: Mon, 10 Sep 2018 22:18:54 +0200 Subject: [PATCH 9/9] c++11 breadth-first search --- .../c++11/01_breadth-first_search.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 06_breadth-first_search/c++11/01_breadth-first_search.cpp 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 00000000..2053590e --- /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