Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added recursive string length and interpolation search Algoritm #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions Algorithms/Recurssion/String_length_recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// CPP program to calculate length of
// a string using recursion
#include <bits/stdc++.h>
using namespace std;

/* Function to calculate length */
int recLen(char* str)
{
// if we reach at the end of the string
if (*str == '\0')
return 0;
else
return 1 + recLen(str + 1);
}

/* Driver program to test above function */
int main()
{
char str[] = "GeeksforGeeks";
cout << recLen(str);
return 0;
}
94 changes: 94 additions & 0 deletions Algorithms/SearchingALgo/interpolation_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//Algoritm-----------------------------------------------
// The idea of formula is to return higher value of pos
// when element to be searched is closer to arr[hi]. And
// smaller value when closer to arr[lo]
//pos = lo + [ (x - arr[lo]) * (hi - lo) / (arr[hi] - arr[Lo]) ]
/*
arr[] ==> Array where elements need to be searched
x ==> Element to be searched
lo ==> Starting index in arr[]
hi ==> Ending index in arr[]


Let's assume that the elements of the array are linearly distributed.

General equation of line : y = m*x + c.
y is the value in the array and x is its index.

Now putting value of lo,hi and x in the equation
arr[hi] = m*hi+c ----(1)
arr[lo] = m*lo+c ----(2)
x = m*pos + c ----(3)

m = (arr[hi] - arr[lo] )/ (hi - lo)

subtracting eqxn (2) from (3)
x - arr[lo] = m * (pos - lo)
lo + (x - arr[lo])/m = pos
pos = lo + (x - arr[lo]) *(hi - lo)/(arr[hi] - arr[lo])


*/

// C++ program to implement interpolation
// search with recursion
#include <bits/stdc++.h>
using namespace std;

// If x is present in arr[0..n-1], then returns
// index of it, else returns -1.
int interpolationSearch(int arr[], int lo, int hi, int x)
{
int pos;

// Since array is sorted, an element present
// in array must be in range defined by corner
if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {

// Probing the position with keeping
// uniform distribution in mind.
pos = lo
+ (((double)(hi - lo) / (arr[hi] - arr[lo]))
* (x - arr[lo]));

// Condition of target found
if (arr[pos] == x)
return pos;

// If x is larger, x is in right sub array
if (arr[pos] < x)
return interpolationSearch(arr, pos + 1, hi, x);

// If x is smaller, x is in left sub array
if (arr[pos] > x)
return interpolationSearch(arr, lo, pos - 1, x);
}
return -1;
}

// Driver Code
int main()
{

// Array of items on which search will
// be conducted.
int arr[] = { 10, 12, 13, 16, 18, 19, 20, 21,
22, 23, 24, 33, 35, 42, 47
};

int n = sizeof(arr) / sizeof(arr[0]);

// Element to be searched
int x = 18;
int index = interpolationSearch(arr, 0, n - 1, x);

// If element was found
if (index != -1)
cout << "Element found at index " << index;
else
cout << "Element not found.";

return 0;
}

// This code is contributed by equbalzeeshan
40 changes: 0 additions & 40 deletions Data Structure/559_Maximum_Depth_of_N-ary_Tree.java

This file was deleted.

40 changes: 0 additions & 40 deletions Data Structure/589_N-ary_Tree_Preorder_Traversal.java

This file was deleted.

38 changes: 0 additions & 38 deletions Data Structure/590_N-ary_Tree_Postorder_Traversal.java

This file was deleted.

115 changes: 0 additions & 115 deletions Data Structure/Array/MatrixMultiplication.c

This file was deleted.

37 changes: 0 additions & 37 deletions Data Structure/Array/MissingNumberInArray.cpp

This file was deleted.

Loading