📌  相关文章
📜  在C中实现upper_bound()和lower_bound()

📅  最后修改于: 2021-05-13 23:14:52             🧑  作者: Mango

给定一个由N个整数和数字K组成的排序数组arr [] ,任务是编写C程序以在给定数组中找到K的upper_bound()和lower_bound()。

例子:

方法:想法是使用二进制搜索。步骤如下:

  • 对于lower_bound():
    1. startIndex初始化为0 ,将endIndex初始化为N – 1
    2. K与数组的中间元素(例如arr [mid] )进行比较。
    3. 如果中间元素大于等于K,则将endIndex更新为中间索引( mid )。
    4. 否则将startIndex更新为中+ 1。
    5. 重复上述步骤,直到startIndex小于endIndex为止
    6. 完成上述所有步骤后, startIndex是给定数组中K的lower_bound。
  • 对于upper_bound():
    1. startIndex初始化为0 ,将endIndex初始化为N – 1
    2. K与数组的中间元素(例如arr [mid] )进行比较。
    3. 如果中间元素小于等于K,则将startIndex更新为中间索引+ 1( mid + 1)。
    4. 否则将endIndex更新为中点。
    5. 重复上述步骤,直到startIndex小于endIndex为止
    6. 完成上述所有步骤后, startIndex是给定数组中K的upper_bound。

下面是上述方法的迭代和递归实现:

Iterative Solution
// C program for iterative implementation
// of the above approach
  
#include 
  
// Function to implement lower_bound
int lower_bound(int arr[], int N, int X)
{
    int mid;
  
    // Initialise starting index and
    // ending index
    int low = 0;
    int high = N;
  
    // Till low is less than high
    while (low < high) {
        mid = low + (high - low) / 2;
  
        // If X is less than or equal
        // to arr[mid], then find in
        // left subarray
        if (X <= arr[mid]) {
            high = mid;
        }
  
        // If X is greater arr[mid]
        // then find in right subarray
        else {
            low = mid + 1;
        }
    }
  
    // Return the lower_bound index
    return low;
}
  
// Function to implement upper_bound
int upper_bound(int arr[], int N, int X)
{
    int mid;
  
    // Initialise starting index and
    // ending index
    int low = 0;
    int high = N;
  
    // Till low is less than high
    while (low < high) {
        // Find the middle index
        mid = low + (high - low) / 2;
  
        // If X is greater than or equal
        // to arr[mid] then find
        // in right subarray
        if (X >= arr[mid]) {
            low = mid + 1;
        }
  
        // If X is less than arr[mid]
        // then find in left subarray
        else {
            high = mid;
        }
    }
  
    // Return the upper_bound index
    return low;
}
  
// Function to implement lower_bound
// and upper_bound of X
void printBound(int arr[], int N, int X)
{
    int idx;
  
    // If lower_bound doesn't exists
    if (lower_bound(arr, N, X) == N) {
        printf("Lower bound doesn't exist");
    }
    else {
  
        // Find lower_bound
        idx = lower_bound(arr, N, X);
        printf("Lower bound of %d is"
               "% d at index % d\n ",
               X,
               arr[idx], idx);
    }
  
    // If upper_bound doesn't exists
    if (upper_bound(arr, N, X) == N) {
        printf("Upper bound doesn't exist");
    }
    else {
  
        // Find upper_bound
        idx = upper_bound(arr, N, X);
        printf("Upper bound of %d is"
               "% d at index % d\n ",
               X,
               arr[idx], idx);
    }
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 4, 6, 10, 12, 18, 20 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Element whose lower bound and
    // upper bound to be found
    int X = 6;
  
    // Function Call
    printBound(arr, N, X);
    return 0;
}


Recursive Solution
// C program for recursive implementation
// of the above approach
  
#include 
  
// Recursive implementation of
// lower_bound
int lower_bound(int arr[], int low,
                int high, int X)
{
  
    // Base Case
    if (low > high) {
        return low;
    }
  
    // Find the middle index
    int mid = low + (high - low) / 2;
  
    // If arr[mid] is greater than
    // or equal to X then search
    // in left subarray
    if (arr[mid] >= X) {
        return lower_bound(arr, low,
                           mid - 1, X);
    }
  
    // If arr[mid] is less than X
    // then search in right subarray
    return lower_bound(arr, mid + 1,
                       high, X);
}
  
// Recursive implementation of
// upper_bound
int upper_bound(int arr[], int low,
                int high, int X)
{
  
    // Base Case
    if (low > high)
        return low;
  
    // Find the middle index
    int mid = low + (high - low) / 2;
  
    // If arr[mid] is less than
    // or equal to X search in
    // right subarray
    if (arr[mid] <= X) {
        return upper_bound(arr, mid + 1,
                           high, X);
    }
  
    // If arr[mid] is greater than X
    // then search in left subarray
    return upper_bound(arr, low,
                       mid - 1, X);
}
  
// Function to implement lower_bound
// and upper_bound of X
void printBound(int arr[], int N, int X)
{
    int idx;
  
    // If lower_bound doesn't exists
    if (lower_bound(arr, 0, N, X) == N) {
        printf("Lower bound doesn't exist");
    }
    else {
  
        // Find lower_bound
        idx = lower_bound(arr, 0, N, X);
        printf("Lower bound of %d "
               "is %d at index %d\n",
               X, arr[idx], idx);
    }
  
    // If upper_bound doesn't exists
    if (upper_bound(arr, 0, N, X) == N) {
        printf("Upper bound doesn't exist");
    }
    else {
  
        // Find upper_bound
        idx = upper_bound(arr, 0, N, X);
        printf("Upper bound of %d is"
               "% d at index % d\n ",
               X,
               arr[idx], idx);
    }
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 4, 6, 10, 12, 18, 20 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Element whose lower bound and
    // upper bound to be found
    int X = 6;
  
    // Function Call
    printBound(arr, N, X);
    return 0;
}


输出:
Lower bound of 6 is 6 at index  1
 Upper bound of 6 is 10 at index  2

时间复杂度: O(log 2 N) ,其中N是数组中元素的数量。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。