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,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)