Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions 01_introduction_to_algorithms/c++11/01_binary_search.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions 01_introduction_to_algorithms/c++11/Makefile
Original file line number Diff line number Diff line change
@@ -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)
50 changes: 50 additions & 0 deletions 02_selection_sort/c++11/01_selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions 02_selection_sort/c++11/Makefile
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions 03_recursion/c++11/01_countdown.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
27 changes: 27 additions & 0 deletions 03_recursion/c++11/02_greet.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions 03_recursion/c++11/03_factorial.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions 04_quicksort/c++11/01_loop_sum.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions 04_quicksort/c++11/02_recursive_sum.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions 04_quicksort/c++11/03_recursive_count.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions 04_quicksort/c++11/04_recursive_max.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions 04_quicksort/c++11/05_quicksort.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions 05_hash_tables/c++11/01_price_of_groceries.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions 05_hash_tables/c++11/02_check_voter.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
Loading