mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-27 22:24:29 +08:00
Add files via upload
This commit is contained in:
34
Search/Binary Search.cpp
Normal file
34
Search/Binary Search.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int binary_search(int a[],int l,int r,int key){
|
||||
while(l<=r){
|
||||
int m = l+(r-l)/2;
|
||||
if(key==a[m])
|
||||
return m;
|
||||
else if(key<a[m])
|
||||
r = m-1;
|
||||
else
|
||||
l = m+1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int n,key;
|
||||
cout<<"Enter size of array: ";
|
||||
cin>>n;
|
||||
cout<<"Enter array elements: ";
|
||||
int a[n];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin>>a[i];
|
||||
}
|
||||
cout<<"Enter search key: ";
|
||||
cin>>key;
|
||||
int res = binary_search(a,0,n-1,key);
|
||||
if(res != -1)
|
||||
cout<<key<<" found at index "<<res<<endl;
|
||||
else
|
||||
cout<<key<<" not found"<<endl;
|
||||
return 0;
|
||||
}
|
||||
43
Search/Linear Search.cpp
Normal file
43
Search/Linear Search.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int LinearSearch(int *array, int key)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
if (array[i]==key)
|
||||
{
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int array[10];
|
||||
int key;
|
||||
|
||||
//Input array
|
||||
cout<<"\nEnter the Array of 10 numbers : ";
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
cin>>array[i];
|
||||
}
|
||||
cout<<"\nEnter the number to be searched : ";
|
||||
cin>>key;
|
||||
|
||||
int index=LinearSearch(array, key);
|
||||
if (index!=-1)
|
||||
{
|
||||
cout<<"\nNumber found at index : "<<index;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\nNot found";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
39
Search/searching.cpp
Normal file
39
Search/searching.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
char paragraph;
|
||||
|
||||
int main()
|
||||
{
|
||||
string paragraph;
|
||||
cout << "Please enter your paragraph: \n";
|
||||
getline (cin,paragraph);
|
||||
cout << "\nHello, your paragraph is:\n " << paragraph << "!\n";
|
||||
cout << "\nThe size of your paragraph = " << paragraph.size() << " characters. \n\n";
|
||||
|
||||
if (paragraph.empty())
|
||||
{
|
||||
cout << "\nThe paragraph is empty" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (true) {
|
||||
string word;
|
||||
cout << "Please enter the word you are searching for: ";
|
||||
getline (cin,word);
|
||||
cout << "Hello, your word is " << word << "!\n";
|
||||
if (paragraph.find(word) == string::npos)
|
||||
{
|
||||
cout << word << " does not exist in the sentence" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl << endl;
|
||||
}
|
||||
system("pause");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
116
Search/ternary_search.cpp
Normal file
116
Search/ternary_search.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* This is a divide and conquer algorithm.
|
||||
* It does this by dividing the search space by 3 parts and
|
||||
* using its property (usually monotonic property) to find
|
||||
* the desired index.
|
||||
*
|
||||
* Time Complexity : O(log3 n)
|
||||
* Space Complexity : O(1) (without the array)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* The absolutePrecision can be modified to fit preference but
|
||||
* it is recommended to not go lower than 10 due to errors that
|
||||
* may occur.
|
||||
*
|
||||
* The value of _target should be decided or can be decided later
|
||||
* by using the variable of the function.
|
||||
*/
|
||||
|
||||
#define _target 10
|
||||
#define absolutePrecision 10
|
||||
#define MAX 10000000
|
||||
|
||||
int N = 21;
|
||||
int A[MAX] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,10};
|
||||
|
||||
/*
|
||||
* get_input function is to receive input from standard IO
|
||||
*/
|
||||
void get_input()
|
||||
{
|
||||
// TODO: Get input from STDIO or write input to memory as done above.
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This is the iterative method of the ternary search which returns the index of the element.
|
||||
*/
|
||||
int it_ternary_search(int left, int right, int A[],int target)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if(left<right)
|
||||
{
|
||||
if(right-left < absolutePrecision)
|
||||
{
|
||||
for(int i=left;i<=right;i++)
|
||||
if(A[i] == target) return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int oneThird = (left+right)/3+1;
|
||||
int twoThird = (left+right)*2/3+1;
|
||||
|
||||
if(A[oneThird] == target) return oneThird;
|
||||
else if(A[twoThird] == target) return twoThird;
|
||||
|
||||
else if(target > A[twoThird]) left = twoThird+1;
|
||||
else if(target < A[oneThird]) right = oneThird-1;
|
||||
|
||||
else left = oneThird+1, right = twoThird-1;
|
||||
}
|
||||
else return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the recursive method of the ternary search which returns the index of the element.
|
||||
*/
|
||||
int rec_ternary_search(int left, int right, int A[],int target)
|
||||
{
|
||||
if(left<right)
|
||||
{
|
||||
if(right-left < absolutePrecision)
|
||||
{
|
||||
for(int i=left;i<=right;i++)
|
||||
if(A[i] == target) return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int oneThird = (left+right)/3+1;
|
||||
int twoThird = (left+right)*2/3+1;
|
||||
|
||||
if(A[oneThird] == target) return oneThird;
|
||||
if(A[twoThird] == target) return twoThird;
|
||||
|
||||
if(target < A[oneThird]) return rec_ternary_search(left, oneThird-1, A, target);
|
||||
if(target > A[twoThird]) return rec_ternary_search(twoThird+1, right, A, target);
|
||||
|
||||
return rec_ternary_search(oneThird+1, twoThird-1, A, target);
|
||||
}
|
||||
else return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* ternary_search is a template function
|
||||
* You could either use it_ternary_search or rec_ternary_search according to preference.
|
||||
*/
|
||||
void ternary_search(int N,int A[],int target)
|
||||
{
|
||||
cout << it_ternary_search(0,N-1,A,target) << '\t';
|
||||
cout << rec_ternary_search(0,N-1,A,target) << '\t';
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
get_input();
|
||||
ternary_search(N,A,_target);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user