Skip to content

Commit 7dc9e95

Browse files
umatbroegonSchiele
authored andcommitted
add c++11 (egonSchiele#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
1 parent 38f5b27 commit 7dc9e95

File tree

15 files changed

+432
-0
lines changed

15 files changed

+432
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using std::cout;
5+
using std::endl;
6+
7+
template <typename T>
8+
int binary_search(const std::vector<T>& list, const int& item) {
9+
int low = 0;
10+
int high = list.size() - 1;
11+
12+
while (low <= high) {
13+
int mid = (low + high) / 2;
14+
T guess = list[mid];
15+
16+
if (guess == item) {
17+
return mid;
18+
}
19+
20+
if (guess > item) {
21+
high = mid - 1;
22+
} else {
23+
low = mid + 1;
24+
}
25+
}
26+
27+
return -1;
28+
}
29+
30+
// this function returns pointer to the found element rather than array index
31+
template <typename T>
32+
const T* binary_search2(const std::vector<T>& list, const T& item) {
33+
const T* low = &list.front();
34+
const T* high = &list.back();
35+
36+
while (low <= high) {
37+
// "guess" is the element in the middle between "high" and "low"
38+
const T* guess = low + ((high - low) / 2);
39+
40+
if (*guess == item)
41+
return guess;
42+
43+
if (*guess > item) {
44+
high = guess - 1;
45+
} else {
46+
low = guess + 1;
47+
}
48+
}
49+
50+
return nullptr;
51+
}
52+
53+
int main() {
54+
std::vector<int> my_list = {1, 3, 5, 7, 9};
55+
const int* binary_search2_result = binary_search2(my_list, 9);
56+
const int* binary_search2_null = binary_search2(my_list, 4); // test finding element that is not in the list
57+
58+
cout << "Binary search for number 3: " << binary_search(my_list, 3) << endl;
59+
cout << "Binary search 2 for number 9 (memory address): " << binary_search2_result << endl;
60+
cout << "Binary search 2 for number 9 (value): " << *binary_search2_result << endl;
61+
62+
if (binary_search2_null == nullptr) {
63+
cout << "4 was not found in the list" << endl;
64+
}
65+
66+
return 0;
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CPP=g++
2+
CPPFLAGS=-Wall -std=c++11
3+
MAIN_NAME=main
4+
objects=01_binary_search.o
5+
6+
main: $(objects)
7+
$(CPP) -std=c++11 $(CPPFLAGS) -o $(MAIN_NAME) $(objects)
8+
9+
clean:
10+
rm -f $(MAIN_NAME) $(objects)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using std::cout;
5+
using std::endl;
6+
7+
// Finds the smallest value in an array
8+
template <typename T>
9+
int find_smallest(const std::vector<T>& arr) {
10+
// stores smallest value
11+
T smallest = arr[0];
12+
// stores index of the smallest value
13+
int smallest_index = 0;
14+
15+
for (int i = 0; i < arr.size(); i++) {
16+
if (arr[i] < smallest) {
17+
smallest = arr[i];
18+
smallest_index = i;
19+
}
20+
}
21+
22+
return smallest_index;
23+
}
24+
25+
template <typename T>
26+
std::vector<T> selection_sort(std::vector<T> arr) {
27+
std::vector<T> sorted;
28+
29+
while(!arr.empty()) {
30+
// find smallest element and add it to sorted array
31+
int smallest_index = find_smallest(arr);
32+
sorted.push_back(arr[smallest_index]);
33+
34+
// remove smallest element from non-sorted array
35+
arr.erase(arr.begin() + smallest_index);
36+
}
37+
38+
return sorted;
39+
}
40+
41+
int main() {
42+
std::vector<float> arr = {1.2, 1.0, 3, 0, -1, 0.5, 100, -99};
43+
std::vector<float> sorted = selection_sort(arr);
44+
45+
cout << "Sorted array: ";
46+
for (float num : sorted) {
47+
cout << num << " ";
48+
}
49+
cout << endl;
50+
}

02_selection_sort/c++11/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CPP=g++
2+
CPPFLAGS=-Wall -std=c++11
3+
MAIN_NAME=main
4+
objects=01_selection_sort.o
5+
6+
main: $(objects)
7+
$(CPP) -std=c++11 $(CPPFLAGS) -o $(MAIN_NAME) $(objects)
8+
9+
clean:
10+
rm -f $(MAIN_NAME) $(objects)

03_recursion/c++11/01_countdown.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
using std::endl;
5+
6+
void countdown(const int& i) {
7+
cout << i << endl;
8+
9+
// base case
10+
if (i <= 0) return;\
11+
12+
// recursive case
13+
countdown(i - 1);
14+
}
15+
16+
int main() {
17+
countdown(5);
18+
}

03_recursion/c++11/02_greet.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using std::cout;
5+
using std::endl;
6+
7+
void greet2(std::string name) {
8+
cout << "How are you, " + name + "?" << endl;
9+
}
10+
11+
void bye() {
12+
cout << "Ok, bye!" << endl;
13+
}
14+
15+
16+
void greet(std::string name) {
17+
cout << "Hello, " + name + "!" << endl;
18+
greet2(name);
19+
cout << "Getting ready to say bye..." << endl;
20+
}
21+
22+
23+
int main() {
24+
greet("Adit");
25+
26+
return 0;
27+
}

03_recursion/c++11/03_factorial.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
using std::endl;
5+
6+
int fact(const int& x) {
7+
if (x == 1) return 1;
8+
return fact(x-1) * x;
9+
}
10+
11+
int main() {
12+
cout << fact(5) << endl;
13+
}

04_quicksort/c++11/01_loop_sum.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using std::cout;
5+
using std::endl;
6+
7+
template <typename T>
8+
T sum(const std::vector<T>& arr) {
9+
T sum = 0;
10+
for (T item : arr) {
11+
sum += item;
12+
}
13+
14+
return sum;
15+
}
16+
17+
int main() {
18+
std::vector<int> arr_int = {1, 2, 3, 4};
19+
std::vector<float> arr_float = {0.1, 0.2, 0.3, 0.4, 0.5};
20+
21+
cout << "Sum ints: " << sum(arr_int) << endl;
22+
cout << "Sum floats: " << sum(arr_float) << endl;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using std::cout;
5+
using std::endl;
6+
7+
template <typename T>
8+
T sum(std::vector<T> arr) {
9+
if (arr.empty()) return 0;
10+
11+
T last_num = arr.back(); // save last number value
12+
arr.pop_back(); // and remove it from array for next recursive call
13+
return first_num + sum(arr);
14+
}
15+
16+
int main() {
17+
std::vector<int> arr_int = {1, 2, 3, 4};
18+
std::vector<float> arr_float = {0.1, 0.2, 0.3, 0.4, 0.5};
19+
20+
cout << "Sum ints: " << sum(arr_int) << endl;
21+
cout << "Sum floats: " << sum(arr_float) << endl;
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using std::cout;
5+
using std::endl;
6+
7+
template <typename T>
8+
int count(std::vector<T> arr) {
9+
if (arr.empty()) return 0;
10+
arr.pop_back();
11+
return count(arr) + 1;
12+
}
13+
14+
int main() {
15+
std::vector<int> array = {0, 1, 2, 3, 4, 5};
16+
cout << count(array) << endl;
17+
}

0 commit comments

Comments
 (0)