Searching and Sorting
This commit is contained in:
144
Searching and Sorting/Searching and Sorting.cpp
Normal file
144
Searching and Sorting/Searching and Sorting.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
template <typename T>
|
||||
void PrintVector(std::vector<T> 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<int> SelectionSort(std::vector<int> 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<int> InsersionSort(std::vector<int> 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<int> BubbleSort(std::vector<int> 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<int> vals, int lookup)
|
||||
{
|
||||
for (const auto& i : vals)
|
||||
{
|
||||
if (i == lookup)
|
||||
{
|
||||
return lookup;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::vector<int> SubsetVector(std::vector<int> in, int start, int end)
|
||||
{
|
||||
std::vector<int> result(start - end + 1);
|
||||
std::copy(in.begin() + start, in.begin() + end + 1, result.begin());
|
||||
return result;
|
||||
}
|
||||
|
||||
int BinarySearch(std::vector<int> 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<int> 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<int> 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;
|
||||
}
|
||||
|
||||
31
Searching and Sorting/Searching and Sorting.sln
Normal file
31
Searching and Sorting/Searching and Sorting.sln
Normal file
@@ -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
|
||||
147
Searching and Sorting/Searching and Sorting.vcxproj
Normal file
147
Searching and Sorting/Searching and Sorting.vcxproj
Normal file
@@ -0,0 +1,147 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{aadb9334-28dc-4954-844c-40fe45e480ce}</ProjectGuid>
|
||||
<RootNamespace>SearchingandSorting</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Searching and Sorting.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user