Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions SearchingAlgorithms/BinarySearch of Arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <string>


using namespace std;
//===========BinarySearch for Static Array=============
int BinarySearch(int arr[], int begin, int n, int item){
if(n == 0)
return -1;
else{
int lastindex = n + begin -1;
if(item == arr[begin])
return begin;
else if(item == arr[lastindex])
return lastindex;
else{
int middle = (n/2 -1) + begin;
if(item > arr[middle])
BinarySearch(arr, middle+1 , n/2 , item);
else if(item == arr[middle])
return arr[middle];
else
BinarySearch(arr, begin , n/2 , item);
}
}
}

//===========BinarySearch for Dynamic Array -Vector-=========
int search(int arr[] , int n , int item){
int index {0};
return index = BinarySearch(arr, 0 , n , item);
}

//Binary search for Vector

int BinarySearch(vector <int> arr, int begin, int n, int item){
if(n == 0)
return -1;
else{
int lastindex = n + begin -1;
if(item == arr[begin])
return begin;
else if(item == arr[lastindex])
return lastindex;
else{
int middle = (n/2 -1) + begin;
if(item > arr[middle])
BinarySearch(arr, middle+1 , n/2 , item);
else if(item == arr[middle])
return arr[middle];
else
BinarySearch(arr, begin , n/2 , item);
}
}
}

int search(vector<int> arr , int item){
int n = arr.size();
int index {0};
return index = BinarySearch(arr, 0 , n , item);
}
int main(){
int arr[]{1,4,6,9,12,34};
int n = sizeof(arr) / sizeof(arr[0]);
cout << search(arr ,n, 34) << endl;
cout<<"Hello World!"<<endl;

return 0;
}
112 changes: 112 additions & 0 deletions SearchingAlgorithms/binarySearch/BinarySearch.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
##
## Auto Generated makefile by CodeLite IDE
## any manual changes will be erased
##
## Debug
ProjectName :=BinarySearch
ConfigurationName :=Debug
WorkspaceConfiguration :=Debug
WorkspacePath :="E:/Software Engineering/CPP/WorkSpaces/LeetCode/Searching"
ProjectPath :="E:/Software Engineering/CPP/WorkSpaces/LeetCode/Searching/BinarySearch"
IntermediateDirectory :=$(ConfigurationName)
OutDir := $(IntermediateDirectory)
CurrentFileName :=
CurrentFilePath :=
CurrentFileFullPath :=
User :=moham
Date :=29/10/2022
CodeLitePath :="C:/Program Files/CodeLite"
LinkerName :=C:/mingw64/bin/g++.exe
SharedObjectLinkerName :=C:/mingw64/bin/g++.exe -shared -fPIC
ObjectSuffix :=.o
DependSuffix :=.o.d
PreprocessSuffix :=.i
DebugSwitch :=-g
IncludeSwitch :=-I
LibrarySwitch :=-l
OutputSwitch :=-o
LibraryPathSwitch :=-L
PreprocessorSwitch :=-D
SourceSwitch :=-c
OutputDirectory :=$(IntermediateDirectory)
OutputFile :=$(IntermediateDirectory)/$(ProjectName).exe
Preprocessors :=
ObjectSwitch :=-o
ArchiveOutputSwitch :=
PreprocessOnlySwitch :=-E
ObjectsFileList :="BinarySearch.txt"
PCHCompileFlags :=
MakeDirCommand :=makedir
RcCmpOptions :=
RcCompilerName :=C:/mingw64/bin/windres.exe
LinkOptions :=
IncludePath := $(IncludeSwitch). $(IncludeSwitch).
IncludePCH :=
RcIncludePath :=
Libs :=
ArLibs :=
LibPath := $(LibraryPathSwitch).

##
## Common variables
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables
##
AR := C:/mingw64/bin/ar.exe rcu
CXX := C:/mingw64/bin/g++.exe
CC := C:/mingw64/bin/gcc.exe
CXXFLAGS := -g -O0 -Wall $(Preprocessors)
CFLAGS := -g -O0 -Wall $(Preprocessors)
ASFLAGS :=
AS := C:/mingw64/bin/as.exe


##
## User defined environment variables
##
CodeLiteDir:=C:\Program Files\CodeLite
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix)



Objects=$(Objects0)

##
## Main Build Targets
##
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs
all: $(OutputFile)

$(OutputFile): $(IntermediateDirectory)/.d $(Objects)
@$(MakeDirCommand) $(@D)
@echo "" > $(IntermediateDirectory)/.d
@echo $(Objects0) > $(ObjectsFileList)
$(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions)

MakeIntermediateDirs:
@$(MakeDirCommand) "$(ConfigurationName)"


$(IntermediateDirectory)/.d:
@$(MakeDirCommand) "$(ConfigurationName)"

PreBuild:


##
## Objects
##
$(IntermediateDirectory)/main.cpp$(ObjectSuffix): main.cpp
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/main.cpp$(DependSuffix) -MM main.cpp
$(CXX) $(IncludePCH) $(SourceSwitch) "E:/Software Engineering/CPP/WorkSpaces/LeetCode/Searching/BinarySearch/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main.cpp$(ObjectSuffix) $(IncludePath)
$(IntermediateDirectory)/main.cpp$(PreprocessSuffix): main.cpp
$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp$(PreprocessSuffix) main.cpp


-include $(IntermediateDirectory)/*$(DependSuffix)
##
## Clean
##
clean:
$(RM) -r $(ConfigurationName)/


98 changes: 98 additions & 0 deletions SearchingAlgorithms/binarySearch/BinarySearch.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<CodeLite_Project Name="BinarySearch" Version="11000" InternalType="Console">
<Description/>
<Dependencies/>
<Settings Type="Executable">
<GlobalSettings>
<Compiler Options="" C_Options="" Assembler="">
<IncludePath Value="."/>
</Compiler>
<Linker Options="">
<LibraryPath Value="."/>
</Linker>
<ResourceCompiler Options=""/>
</GlobalSettings>
<Configuration Name="Debug" CompilerType="MinGW ( C:\mingw64\bin\ )" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
<Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="1">
<IncludePath Value="."/>
</Compiler>
<Linker Options="" Required="yes"/>
<ResourceCompiler Options="" Required="no"/>
<General OutputFile="$(IntermediateDirectory)/$(ProjectName).exe" IntermediateDirectory="$(ConfigurationName)" Command="$(OutputFile)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
<BuildSystem Name="Default"/>
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
<![CDATA[]]>
</Environment>
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="no">
<DebuggerSearchPaths/>
<PostConnectCommands/>
<StartupCommands/>
</Debugger>
<PreBuild/>
<PostBuild/>
<CustomBuild Enabled="no">
<RebuildCommand/>
<CleanCommand/>
<BuildCommand/>
<PreprocessFileCommand/>
<SingleFileCommand/>
<MakefileGenerationCommand/>
<ThirdPartyToolName>None</ThirdPartyToolName>
<WorkingDirectory/>
</CustomBuild>
<AdditionalRules>
<CustomPostBuild/>
<CustomPreBuild/>
</AdditionalRules>
<Completion EnableCpp11="no" EnableCpp14="no">
<ClangCmpFlagsC/>
<ClangCmpFlags/>
<ClangPP/>
<SearchPaths/>
</Completion>
</Configuration>
<Configuration Name="Release" CompilerType="MinGW ( C:\mingw64\bin\ )" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
<Compiler Options="-O2;-Wall" C_Options="-O2;-Wall" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="1">
<IncludePath Value="."/>
<Preprocessor Value="NDEBUG"/>
</Compiler>
<Linker Options="" Required="yes"/>
<ResourceCompiler Options="" Required="no"/>
<General OutputFile="$(IntermediateDirectory)/$(ProjectName).exe" IntermediateDirectory="$(ConfigurationName)" Command="$(OutputFile)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
<BuildSystem Name="Default"/>
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
<![CDATA[]]>
</Environment>
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="no">
<DebuggerSearchPaths/>
<PostConnectCommands/>
<StartupCommands/>
</Debugger>
<PreBuild/>
<PostBuild/>
<CustomBuild Enabled="no">
<RebuildCommand/>
<CleanCommand/>
<BuildCommand/>
<PreprocessFileCommand/>
<SingleFileCommand/>
<MakefileGenerationCommand/>
<ThirdPartyToolName>None</ThirdPartyToolName>
<WorkingDirectory/>
</CustomBuild>
<AdditionalRules>
<CustomPostBuild/>
<CustomPreBuild/>
</AdditionalRules>
<Completion EnableCpp11="no" EnableCpp14="no">
<ClangCmpFlagsC/>
<ClangCmpFlags/>
<ClangPP/>
<SearchPaths/>
</Completion>
</Configuration>
</Settings>
<VirtualDirectory Name="src">
<File Name="main.cpp"/>
</VirtualDirectory>
</CodeLite_Project>
1 change: 1 addition & 0 deletions SearchingAlgorithms/binarySearch/BinarySearch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Debug/main.cpp.o
9 changes: 9 additions & 0 deletions SearchingAlgorithms/binarySearch/compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-IE:\Software Engineering\CPP\WorkSpaces\LeetCode\Searching\BinarySearch
-IC:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++
-IC:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\x86_64-w64-mingw32
-IC:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\backward
-IC:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include
-IC:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include-fixed
-IC:\mingw64\x86_64-w64-mingw32\include
-target
x86_64-w64-mingw32
70 changes: 70 additions & 0 deletions SearchingAlgorithms/binarySearch/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <string>


using namespace std;
//===========BinarySearch for Static Array=============
int BinarySearch(int arr[], int begin, int n, int item){
if(n == 0)
return -1;
else{
int lastindex = n + begin -1;
if(item == arr[begin])
return begin;
else if(item == arr[lastindex])
return lastindex;
else{
int middle = (n/2 -1) + begin;
if(item > arr[middle])
BinarySearch(arr, middle+1 , n/2 , item);
else if(item == arr[middle])
return arr[middle];
else
BinarySearch(arr, begin , n/2 , item);
}
}
}

//===========BinarySearch for Dynamic Array -Vector-=========
int search(int arr[] , int n , int item){
int index {0};
return index = BinarySearch(arr, 0 , n , item);
}

//Binary search for Vector

int BinarySearch(vector <int> arr, int begin, int n, int item){
if(n == 0)
return -1;
else{
int lastindex = n + begin -1;
if(item == arr[begin])
return begin;
else if(item == arr[lastindex])
return lastindex;
else{
int middle = (n/2 -1) + begin;
if(item > arr[middle])
BinarySearch(arr, middle+1 , n/2 , item);
else if(item == arr[middle])
return arr[middle];
else
BinarySearch(arr, begin , n/2 , item);
}
}
}

int search(vector<int> arr , int item){
int n = arr.size();
int index {0};
return index = BinarySearch(arr, 0 , n , item);
}
int main(){
int arr[]{1,4,6,9,12,34};
int n = sizeof(arr) / sizeof(arr[0]);
cout << search(arr ,n, 34) << endl;
cout<<"Hello World!"<<endl;

return 0;
}