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:
50
02_selection_sort/c++11/01_selection_sort.cpp
Normal file
50
02_selection_sort/c++11/01_selection_sort.cpp
Normal 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;
|
||||
}
|
||||
10
02_selection_sort/c++11/Makefile
Normal file
10
02_selection_sort/c++11/Makefile
Normal 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)
|
||||
Reference in New Issue
Block a user