diff --git a/Searching and Sorting/Searching and Sorting.cpp b/Searching and Sorting/Searching and Sorting.cpp new file mode 100644 index 0000000..d4b33dc --- /dev/null +++ b/Searching and Sorting/Searching and Sorting.cpp @@ -0,0 +1,144 @@ +#include +#include + +template +void PrintVector(std::vector v) +{ + for (const auto& e : v) + std::cout << e << ", "; + std::cout << std::endl; +} + +void Swap(int* a, int* b) +{ + int temp = *b; + *b = *a; + *a = temp; +} + +std::vector SelectionSort(std::vector in) +{ + for (int i = 0; i < in.size(); i++) + { + int indexOfSmallest = i; + for (int j = i; j < in.size(); j++) + { + if (in[indexOfSmallest] > in[j]) + { + indexOfSmallest = j; + } + } + if (indexOfSmallest != i) + { + Swap(&in[i], &in[indexOfSmallest]); + } + } + return in; +} + +std::vector InsersionSort(std::vector in) +{ + for (int i = 0; i < in.size(); i++) + { + int itemToInsert = in[i]; + int indexHole = i; + while (indexHole > 0 && in[indexHole - 1] > itemToInsert) + { + in[indexHole] = in[indexHole - 1]; + indexHole--; + } + + in[indexHole] = itemToInsert; + } + return in; +} + +std::vector BubbleSort(std::vector in) +{ + for (int i = 0; i < in.size(); i++) + { + for (int j = 0; j < in.size() - 1; j++) + { + if (in[j] > in[j + 1]) + { + Swap(&in[j], &in[j + 1]); + } + } + } + return in; +} + +int LinearSearch(std::vector vals, int lookup) +{ + for (const auto& i : vals) + { + if (i == lookup) + { + return lookup; + } + } +} + + +std::vector SubsetVector(std::vector in, int start, int end) +{ + std::vector result(start - end + 1); + std::copy(in.begin() + start, in.begin() + end + 1, result.begin()); + return result; +} + +int BinarySearch(std::vector set, int lookup) +{ + if (set[set.size() / 2] == lookup) return lookup; + if (set[set.size() / 2] > lookup) return BinarySearch(SubsetVector(set, set.size() / 2, set.size()), lookup); + if (set[set.size() / 2] < lookup) return BinarySearch(SubsetVector(set, 0, set.size() / 2), lookup); +} + + +int TernarySearch(std::vector set, int lookup) +{ + if (set.size() == 1) return set[0]; + if (set.size() == 2) return BinarySearch(set, lookup); + + int firstThird = 0; + int secondThird = set.size() / 3; + int thirdThird = (set.size() / 3) * 2; + int lastThird = set.size(); + + if (set[firstThird] > lookup && set[secondThird] < lookup) return TernarySearch(SubsetVector(set, firstThird, secondThird), lookup); + if (set[secondThird] > lookup && set[thirdThird] < lookup) return TernarySearch(SubsetVector(set, secondThird, thirdThird), lookup); + if (set[thirdThird] > lookup && set[lastThird] < lookup) return TernarySearch(SubsetVector(set, thirdThird, lastThird), lookup); +} + +int main() +{ + std::vector sorted; + + sorted = SelectionSort({ 1, 4, 3, 5, 7, 4, 3, 7, 8, 2, 10, 5, 100 }); + + PrintVector(sorted); + + sorted = InsersionSort({ 1, 4, 3, 5, 7, 4, 3, 7, 8, 2, 10, 5, 100 }); + + PrintVector(sorted); + + sorted = BubbleSort({ 1, 4, 3, 5, 7, 4, 3, 7, 8, 2, 10, 5, 100 }); + + PrintVector(sorted); + + + std::cout << std::endl; + + + int lookup = 0; + + lookup = LinearSearch(BubbleSort({ 1, 4, 3, 5, 7, 4, 3, 7, 8, 2, 10, 5, 100 }), 5); + std::cout << lookup << std::endl; + + lookup = BinarySearch(BubbleSort({ 1, 4, 3, 5, 7, 4, 3, 7, 8, 2, 10, 5, 100 }), 5); + std::cout << lookup << std::endl; + + lookup = TernarySearch(BubbleSort({ 1, 4, 3, 5, 7, 4, 3, 7, 8, 2, 10, 5, 100 }), 5); + std::cout << lookup << std::endl; +} + diff --git a/Searching and Sorting/Searching and Sorting.sln b/Searching and Sorting/Searching and Sorting.sln new file mode 100644 index 0000000..7c83e3f --- /dev/null +++ b/Searching and Sorting/Searching and Sorting.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31808.319 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Searching and Sorting", "Searching and Sorting.vcxproj", "{AADB9334-28DC-4954-844C-40FE45E480CE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AADB9334-28DC-4954-844C-40FE45E480CE}.Debug|x64.ActiveCfg = Debug|x64 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Debug|x64.Build.0 = Debug|x64 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Debug|x86.ActiveCfg = Debug|Win32 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Debug|x86.Build.0 = Debug|Win32 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Release|x64.ActiveCfg = Release|x64 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Release|x64.Build.0 = Release|x64 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Release|x86.ActiveCfg = Release|Win32 + {AADB9334-28DC-4954-844C-40FE45E480CE}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D94B0734-9957-4BA1-93F4-1E2ED7D91F0B} + EndGlobalSection +EndGlobal diff --git a/Searching and Sorting/Searching and Sorting.vcxproj b/Searching and Sorting/Searching and Sorting.vcxproj new file mode 100644 index 0000000..f2087f6 --- /dev/null +++ b/Searching and Sorting/Searching and Sorting.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {aadb9334-28dc-4954-844c-40fe45e480ce} + SearchingandSorting + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file