Advertisement
AnindyaBiswas

TriSortedArray

Aug 25th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int A[] = {1, 3, 8, 12, 9, 7, 2, 4, 6}, Size = 0; // Will update size in main
  6.  
  7. // Get the first transition index
  8. int findTransition1() {
  9.     int left = 0, right = Size - 1;
  10.  
  11.     while (left < right) {
  12.         int mid = left + (right - left) / 2;
  13.  
  14.         if (A[mid] > A[mid + 1]) {
  15.             right = mid; // Move to left subarray
  16.         } else {
  17.             left = mid + 1; // Move to right subarray
  18.         }
  19.     }
  20.  
  21.     return left;
  22. }
  23.  
  24. // Get the second transition index
  25. int findTransition2() {
  26.     int left = 0, right = Size - 1;
  27.  
  28.     while (left < right) {
  29.         int mid = left + (right - left) / 2;
  30.  
  31.         if (A[mid] < A[mid + 1]) {
  32.             right = mid; // Move to left subarray
  33.         } else {
  34.             left = mid + 1; // Move to right subarray
  35.         }
  36.     }
  37.  
  38.     return left;
  39. }
  40.  
  41. // Binary Search on increasing subarray
  42. int increasingBinarySearch(int left, int right, int x) {
  43.     while (left <= right) {
  44.         int mid = left + (right - left) / 2;
  45.  
  46.         if (A[mid] == x)
  47.             return mid;
  48.         if (A[mid] < x)
  49.             left = mid + 1;
  50.         else
  51.             right = mid - 1;
  52.     }
  53.  
  54.     return -1;
  55. }
  56.  
  57. // Binary Search on decreasing subarray
  58. int decreasingBinarySearch(int left, int right, int x) {
  59.     while (left <= right) {
  60.         int mid = left + (right - left) / 2;
  61.  
  62.         if (A[mid] == x)
  63.             return mid;
  64.         if (A[mid] > x)
  65.             left = mid + 1;
  66.         else
  67.             right = mid - 1;
  68.     }
  69.  
  70.     return -1;
  71. }
  72.  
  73. // Function to search x in a TriSorted array
  74. int triSortedArraySearch(int x) {
  75.     int transition1 = findTransition1(), transition2 = findTransition2();
  76.     cout<<"Transition Indices: "<<transition1<<" "<<transition2<<endl;
  77.    
  78.     // Search in the increasing part 1
  79.     int result = increasingBinarySearch(0, transition1-1, x);
  80.     if (result != -1)
  81.         return result ;
  82.     // Search in the decreasing part
  83.     result = decreasingBinarySearch(transition1, transition2-1, x);
  84.     if(result != -1)
  85.         return result;
  86.     // Search in the increasing part 2
  87.     result = increasingBinarySearch(transition2, Size-1, x);
  88.     return result;
  89.        
  90. }
  91.  
  92. int main() {
  93.  
  94.     Size = sizeof(A) / sizeof(A[0]);
  95.     int x = 9;
  96.     int result = triSortedArraySearch(x);
  97.     if (result != -1) {
  98.         cout << "Element " << x << " found at index " << result << endl;
  99.     } else {
  100.         cout << "Element " << x << " not found." << endl;
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement