From 8ac96885d33f7eb6639d15f6f99f6c70f2d4e772 Mon Sep 17 00:00:00 2001 From: SHAKILEASH <52775046+SHAKILEASH@users.noreply.github.com> Date: Mon, 15 Jul 2019 23:04:00 +0530 Subject: [PATCH 1/5] Interpolation Search is included --- .../Interpolation_search.cpp | 59 ++++++++++++ Search/Interpolation_Search/README.md | 19 +++- Search/Ternary_Search/New Text Document.txt | 92 +++++++++++++++++++ Search/Ternary_Search/Ternary search.cpp | 92 +++++++++++++++++++ 4 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 Search/Interpolation_Search/Interpolation_search.cpp create mode 100644 Search/Ternary_Search/New Text Document.txt create mode 100644 Search/Ternary_Search/Ternary search.cpp diff --git a/Search/Interpolation_Search/Interpolation_search.cpp b/Search/Interpolation_Search/Interpolation_search.cpp new file mode 100644 index 0000000..56df080 --- /dev/null +++ b/Search/Interpolation_Search/Interpolation_search.cpp @@ -0,0 +1,59 @@ +// C++ program to implement interpolation search +#include +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 n, int x) +{ + // Find indexes of two corners + int lo = 0, hi = (n - 1); + + // Since array is sorted, an element present + // in array must be in range defined by corner + while (lo <= hi && x >= arr[lo] && x <= arr[hi]) + { + if (lo == hi) + { + if (arr[lo] == x) return lo; + return -1; + } + // Probing the position with keeping + // uniform distribution in mind. + int 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 upper part + if (arr[pos] < x) + lo = pos + 1; + + // If x is smaller, x is in the lower part + else + hi = pos - 1; + } + 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]); + + int x = 18; // Element to be searched + int index = interpolationSearch(arr, n, x); + + // If element was found + if (index != -1) + cout << "Element found at index " << index; + else + cout << "Element not found."; + return 0; +} \ No newline at end of file diff --git a/Search/Interpolation_Search/README.md b/Search/Interpolation_Search/README.md index 24a90d8..13bc058 100644 --- a/Search/Interpolation_Search/README.md +++ b/Search/Interpolation_Search/README.md @@ -1 +1,18 @@ -Content \ No newline at end of file +# Interpolation Search + The Interpolation Search is an improvement over Binary Search for instances, where the values in a sorted array are uniformly distributed. +--- +## Algorithm +''' + STEP 1:In a loop, calculate the value of “pos” using the probe position formula. + STEP 2:If it is a match, return the index of the item, and exit. + STEP 3:If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. + STEP 4:Repeat until a match is found or the sub-array reduces to zero. + ''' +--- +## Explanation + Binary Search always goes to the middle element to check. + On the other hand, interpolation search may go to different locations according to the value of the key being searched. + For example, if the value of the key is closer to the last element, interpolation search is likely to start search toward the end side. + + +## [Interpolation search c++ implementation](Interpolation_search.cpp) \ No newline at end of file diff --git a/Search/Ternary_Search/New Text Document.txt b/Search/Ternary_Search/New Text Document.txt new file mode 100644 index 0000000..8457486 --- /dev/null +++ b/Search/Ternary_Search/New Text Document.txt @@ -0,0 +1,92 @@ +// recursive approach to ternary search +#include +using namespace std; + +// Function to perform Ternary Search +int ternarySearch(int l, int r, int key, int ar[]) +{ + if (r >= l) + { + + // Find the mid1 nad mid2 + int mid1 = l + (r - l) / 3; + int mid2 = r - (r - l) / 3; + + // Check if key is present at any mid + if (ar[mid1] == key) + { + return mid1; + } + if (ar[mid2] == key) + { + return mid2; + } + + // Since key is not present at mid, + // check in which region it is present + // then repeat the Search operation + // in that region + if (key < ar[mid1]) + { + + // The key lies in between l and mid1 + return ternarySearch(l, mid1 - 1, key, ar); + } + else if (key > ar[mid2]) + { + + // The key lies in between mid2 and r + return ternarySearch(mid2 + 1, r, key, ar); + } + else + { + + // The key lies in between mid1 and mid2 + return ternarySearch(mid1 + 1, mid2 - 1, key, ar); + } + } + + // Key not found + return -1; +} + +// Driver code +int main() +{ + int l, r, p, key; + + // Get the array + // Sort the array if not sorted + int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + // Starting index + l = 0; + + // length of array + r = 9; + + // Checking for 5 + + // Key to be searched in the array + key = 5; + + // Search the key using ternarySearch + p = ternarySearch(l, r, key, ar); + + // Print the result + cout << "Index of " << key + << " is " << p << endl; + + // Checking for 50 + + // Key to be searched in the array + key = 50; + + // Search the key using ternarySearch + p = ternarySearch(l, r, key, ar); + + // Print the result + cout << "Index of " << key + << " is " << p << endl; +} + \ No newline at end of file diff --git a/Search/Ternary_Search/Ternary search.cpp b/Search/Ternary_Search/Ternary search.cpp new file mode 100644 index 0000000..8457486 --- /dev/null +++ b/Search/Ternary_Search/Ternary search.cpp @@ -0,0 +1,92 @@ +// recursive approach to ternary search +#include +using namespace std; + +// Function to perform Ternary Search +int ternarySearch(int l, int r, int key, int ar[]) +{ + if (r >= l) + { + + // Find the mid1 nad mid2 + int mid1 = l + (r - l) / 3; + int mid2 = r - (r - l) / 3; + + // Check if key is present at any mid + if (ar[mid1] == key) + { + return mid1; + } + if (ar[mid2] == key) + { + return mid2; + } + + // Since key is not present at mid, + // check in which region it is present + // then repeat the Search operation + // in that region + if (key < ar[mid1]) + { + + // The key lies in between l and mid1 + return ternarySearch(l, mid1 - 1, key, ar); + } + else if (key > ar[mid2]) + { + + // The key lies in between mid2 and r + return ternarySearch(mid2 + 1, r, key, ar); + } + else + { + + // The key lies in between mid1 and mid2 + return ternarySearch(mid1 + 1, mid2 - 1, key, ar); + } + } + + // Key not found + return -1; +} + +// Driver code +int main() +{ + int l, r, p, key; + + // Get the array + // Sort the array if not sorted + int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + // Starting index + l = 0; + + // length of array + r = 9; + + // Checking for 5 + + // Key to be searched in the array + key = 5; + + // Search the key using ternarySearch + p = ternarySearch(l, r, key, ar); + + // Print the result + cout << "Index of " << key + << " is " << p << endl; + + // Checking for 50 + + // Key to be searched in the array + key = 50; + + // Search the key using ternarySearch + p = ternarySearch(l, r, key, ar); + + // Print the result + cout << "Index of " << key + << " is " << p << endl; +} + \ No newline at end of file From 5104da690aff5753e59a927cc80da8789ff445b5 Mon Sep 17 00:00:00 2001 From: SHAKILEASH <52775046+SHAKILEASH@users.noreply.github.com> Date: Mon, 15 Jul 2019 23:04:44 +0530 Subject: [PATCH 2/5] Interpolation Search is included --- Search/Ternary_Search/New Text Document.txt | 92 --------------------- Search/Ternary_Search/Ternary search.cpp | 92 --------------------- 2 files changed, 184 deletions(-) delete mode 100644 Search/Ternary_Search/New Text Document.txt delete mode 100644 Search/Ternary_Search/Ternary search.cpp diff --git a/Search/Ternary_Search/New Text Document.txt b/Search/Ternary_Search/New Text Document.txt deleted file mode 100644 index 8457486..0000000 --- a/Search/Ternary_Search/New Text Document.txt +++ /dev/null @@ -1,92 +0,0 @@ -// recursive approach to ternary search -#include -using namespace std; - -// Function to perform Ternary Search -int ternarySearch(int l, int r, int key, int ar[]) -{ - if (r >= l) - { - - // Find the mid1 nad mid2 - int mid1 = l + (r - l) / 3; - int mid2 = r - (r - l) / 3; - - // Check if key is present at any mid - if (ar[mid1] == key) - { - return mid1; - } - if (ar[mid2] == key) - { - return mid2; - } - - // Since key is not present at mid, - // check in which region it is present - // then repeat the Search operation - // in that region - if (key < ar[mid1]) - { - - // The key lies in between l and mid1 - return ternarySearch(l, mid1 - 1, key, ar); - } - else if (key > ar[mid2]) - { - - // The key lies in between mid2 and r - return ternarySearch(mid2 + 1, r, key, ar); - } - else - { - - // The key lies in between mid1 and mid2 - return ternarySearch(mid1 + 1, mid2 - 1, key, ar); - } - } - - // Key not found - return -1; -} - -// Driver code -int main() -{ - int l, r, p, key; - - // Get the array - // Sort the array if not sorted - int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - // Starting index - l = 0; - - // length of array - r = 9; - - // Checking for 5 - - // Key to be searched in the array - key = 5; - - // Search the key using ternarySearch - p = ternarySearch(l, r, key, ar); - - // Print the result - cout << "Index of " << key - << " is " << p << endl; - - // Checking for 50 - - // Key to be searched in the array - key = 50; - - // Search the key using ternarySearch - p = ternarySearch(l, r, key, ar); - - // Print the result - cout << "Index of " << key - << " is " << p << endl; -} - \ No newline at end of file diff --git a/Search/Ternary_Search/Ternary search.cpp b/Search/Ternary_Search/Ternary search.cpp deleted file mode 100644 index 8457486..0000000 --- a/Search/Ternary_Search/Ternary search.cpp +++ /dev/null @@ -1,92 +0,0 @@ -// recursive approach to ternary search -#include -using namespace std; - -// Function to perform Ternary Search -int ternarySearch(int l, int r, int key, int ar[]) -{ - if (r >= l) - { - - // Find the mid1 nad mid2 - int mid1 = l + (r - l) / 3; - int mid2 = r - (r - l) / 3; - - // Check if key is present at any mid - if (ar[mid1] == key) - { - return mid1; - } - if (ar[mid2] == key) - { - return mid2; - } - - // Since key is not present at mid, - // check in which region it is present - // then repeat the Search operation - // in that region - if (key < ar[mid1]) - { - - // The key lies in between l and mid1 - return ternarySearch(l, mid1 - 1, key, ar); - } - else if (key > ar[mid2]) - { - - // The key lies in between mid2 and r - return ternarySearch(mid2 + 1, r, key, ar); - } - else - { - - // The key lies in between mid1 and mid2 - return ternarySearch(mid1 + 1, mid2 - 1, key, ar); - } - } - - // Key not found - return -1; -} - -// Driver code -int main() -{ - int l, r, p, key; - - // Get the array - // Sort the array if not sorted - int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - // Starting index - l = 0; - - // length of array - r = 9; - - // Checking for 5 - - // Key to be searched in the array - key = 5; - - // Search the key using ternarySearch - p = ternarySearch(l, r, key, ar); - - // Print the result - cout << "Index of " << key - << " is " << p << endl; - - // Checking for 50 - - // Key to be searched in the array - key = 50; - - // Search the key using ternarySearch - p = ternarySearch(l, r, key, ar); - - // Print the result - cout << "Index of " << key - << " is " << p << endl; -} - \ No newline at end of file From df4d0276eb776920986b8482b4fa04a41337bd6e Mon Sep 17 00:00:00 2001 From: SHAKILEASH <52775046+SHAKILEASH@users.noreply.github.com> Date: Mon, 15 Jul 2019 23:08:39 +0530 Subject: [PATCH 3/5] Update README.md --- Search/Interpolation_Search/README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Search/Interpolation_Search/README.md b/Search/Interpolation_Search/README.md index 13bc058..da741c1 100644 --- a/Search/Interpolation_Search/README.md +++ b/Search/Interpolation_Search/README.md @@ -1,18 +1,19 @@ # Interpolation Search - The Interpolation Search is an improvement over Binary Search for instances, where the values in a sorted array are uniformly distributed. + --- ## Algorithm ''' - STEP 1:In a loop, calculate the value of “pos” using the probe position formula. - STEP 2:If it is a match, return the index of the item, and exit. - STEP 3:If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. - STEP 4:Repeat until a match is found or the sub-array reduces to zero. + STEP 1:In a loop, calculate the value of “pos” using the probe position formula. + STEP 2:If it is a match, return the index of the item, and exit. + STEP 3:If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. + STEP 4:Repeat until a match is found or the sub-array reduces to zero. ''' --- ## Explanation + The Interpolation Search is an improvement over Binary Search for instances, where the values in a sorted array are uniformly distributed. Binary Search always goes to the middle element to check. On the other hand, interpolation search may go to different locations according to the value of the key being searched. For example, if the value of the key is closer to the last element, interpolation search is likely to start search toward the end side. -## [Interpolation search c++ implementation](Interpolation_search.cpp) \ No newline at end of file +## [Interpolation search c++ implementation](Interpolation_search.cpp) From 230a6bf2d53c808e18a3ea0a02918fcb53ffbad6 Mon Sep 17 00:00:00 2001 From: SHAKILEASH <52775046+SHAKILEASH@users.noreply.github.com> Date: Mon, 15 Jul 2019 23:12:26 +0530 Subject: [PATCH 4/5] Update README.md --- Search/Interpolation_Search/README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Search/Interpolation_Search/README.md b/Search/Interpolation_Search/README.md index da741c1..2250be0 100644 --- a/Search/Interpolation_Search/README.md +++ b/Search/Interpolation_Search/README.md @@ -1,13 +1,12 @@ # Interpolation Search ---- ## Algorithm -''' +```c STEP 1:In a loop, calculate the value of “pos” using the probe position formula. STEP 2:If it is a match, return the index of the item, and exit. - STEP 3:If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. + STEP 3:If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. STEP 4:Repeat until a match is found or the sub-array reduces to zero. - ''' + ``` --- ## Explanation The Interpolation Search is an improvement over Binary Search for instances, where the values in a sorted array are uniformly distributed. From 45598bea8ca6ca79329b82b2598e74106a0c7b46 Mon Sep 17 00:00:00 2001 From: SHAKILEASH <52775046+SHAKILEASH@users.noreply.github.com> Date: Mon, 15 Jul 2019 23:16:06 +0530 Subject: [PATCH 5/5] Update Interpolation_search.cpp --- Search/Interpolation_Search/Interpolation_search.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Search/Interpolation_Search/Interpolation_search.cpp b/Search/Interpolation_Search/Interpolation_search.cpp index 56df080..5c47637 100644 --- a/Search/Interpolation_Search/Interpolation_search.cpp +++ b/Search/Interpolation_Search/Interpolation_search.cpp @@ -1,7 +1,7 @@ // C++ program to implement interpolation search -#include -using namespace std; - +#include +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 n, int x) @@ -56,4 +56,4 @@ int main() else cout << "Element not found."; return 0; -} \ No newline at end of file +}