在旋转后的有序数组中查找元素,要求O(logn)的时间复杂度

题:比如说在A[] = {7, 8, 1, 2, 3, 4, 5, 6};查找元素。A是由{1, 2, 3, 4, 5, 6, 7, 8}左旋6位得到。

假定数组旋转前是有序递增的,且没有重复的元素。

方法:二分查找,再分情况讨论。在确定l, m, r后,旋转后的数组有图1所示的四种情况,且对应的元素有图2所示的关系。

由图中可以看出,根据A[l]与A[r]的大小关系可以区分出第4种情况。然后根据A[l]与A[m]的关系可以区分出第1中情况。

第2和第3中情况难以区分,但是如果

(1)m == r,肯定是第2种情况(或第4种情况,当然第4种情况右侧元素个数为0的话也退化成第2种情况)

(2)否则m < r,m+1在检查的范围之内,是有意义的

      1)如果A[m] > A[m + 1]则是第2种情况

      2)否则A[m] < A[m + 1]是第3种情况

 

对于第4和第2种情况,简单的调用通用的二分查找函数即可。

对于第1种情况,如果确定要查找的值在A[m+1:r]的范围内,可以调用简单的二分查找函数,否则对A[l:m+1]继续上面的情况分类。

对于第3种情况,讨论与第1种情况类似。

 

程序如下

[cpp:firstline[1]]  view plain copy
  1. #include <iostream>  
  2. #include <iomanip>  
  3. #include <cstdlib>  
  4. #include <ctime>  
  5. using namespace std;  
  6. static void left_rotate_1(int A[], const int N)  
  7. {  
  8.     int tmp = A[0];  
  9.     for (int i = 0; i < N - 1; i++){  
  10.         A[i] = A[i + 1];  
  11.     }  
  12.     A[N - 1] = tmp;  
  13. }  
  14. static int binary_search(const int A[], int l, int r, const int val)  
  15. {  
  16.     int m;  
  17.     while (l <= r){  
  18.         m = (l + r) >> 1;  
  19.         if (val == A[m])  
  20.             return m;  
  21.         else if (val < A[m])  
  22.             r = m - 1;  
  23.         else  
  24.             l = m + 1;  
  25.     }  
  26.     return -1;  
  27. }  
  28. static int rotated_search(const int A[], const int N,   
  29.         const int val)  
  30. {  
  31.     int l, r, m;  
  32.     l = 0;   
  33.     r = N - 1;  
  34.     cout << "searching " << val << endl;  
  35.     while (l <= r){  
  36.         m = (l + r) >> 1;  
  37.         if (A[l] < A[r]){  
  38.             /*case 4*/  
  39.             return binary_search(A, l, r, val);  
  40.         }  
  41.         if (val == A[m])  
  42.             return m;  
  43.         else if (A[l] > A[m]){  
  44.             /*case 1*/  
  45.             if (val > A[m] && val <= A[r])  
  46.                 return binary_search(A, m + 1, r, val);  
  47.             else   
  48.                 r = m - 1;  
  49.         }else{  
  50.             /*case 2 and 3*/  
  51.             if (m == r || A[m] > A[m + 1]){  
  52.                 /*case 2*/  
  53.                 l = binary_search(A, l, m - 1, val);  
  54.                 if (-1 != l)  
  55.                     return l;  
  56.                 return binary_search(A, m + 1, r, val);  
  57.             }else{  
  58.                 /*case 3*/  
  59.                 if (val >= A[l] && val < A[m]){  
  60.                     return binary_search(A, l, m - 1, val);  
  61.                 }else{  
  62.                     l = m + 1;  
  63.                 }  
  64.             }  
  65.         }  
  66.     }  
  67.     return -1;  
  68. }  
  69. static inline void case_test(const int val)  
  70. {  
  71.     cout << "get" << setw(5) << val << endl;  
  72. }  
  73. static void print_array(const int A[], const int N)  
  74. {  
  75.     cout << "index:";  
  76.     for (int i = 0; i < N; i++){  
  77.         cout << setw(5) << i;  
  78.     }  
  79.     cout << endl;  
  80.     cout << "array:";  
  81.     for (int i = 0; i < N; i++){  
  82.         cout << setw(5) << A[i];  
  83.     }  
  84.     cout << endl;  
  85. }  
  86. static int int_compare(const void *p1, const void *p2)  
  87. {  
  88.     return (*(const int *)p1 - *(const int *)p2);  
  89. }  
  90. int main()  
  91. {  
  92.     const int N = 8;  
  93.     int A[N] = {4, 4, 5, 6, 7, 2, 3, 4};/* = {6, 1, 5, 5, 5, 3, 6, 4};*/  
  94.       
  95.     srandom((unsigned int)time(NULL));  
  96.     print_array(A, N);  
  97.     case_test(rotated_search(A, N, 5));  
  98.     for (int i = 0; i < N; i++)  
  99.         A[i] = random() % N;  
  100.     qsort(A, N, sizeof(int), int_compare);  
  101.       
  102.     print_array(A, N);  
  103.     case_test(rotated_search(A, N, 3));  
  104.     case_test(rotated_search(A, N, random() % N));  
  105.     case_test(rotated_search(A, N, random() % N));  
  106.     case_test(rotated_search(A, N, random() % N));  
  107.     left_rotate_1(A, N);  
  108.     print_array(A, N);  
  109.     case_test(rotated_search(A, N, random() % N));  
  110.     case_test(rotated_search(A, N, random() % N));  
  111.     case_test(rotated_search(A, N, random() % N));  
  112.     case_test(rotated_search(A, N, random() % N));  
  113.     left_rotate_1(A, N);  
  114.     left_rotate_1(A, N);  
  115.     print_array(A, N);  
  116.     case_test(rotated_search(A, N, random() % N));  
  117.     case_test(rotated_search(A, N, random() % N));  
  118.     case_test(rotated_search(A, N, random() % N));  
  119.     case_test(rotated_search(A, N, random() % N));  
  120.     return 0;  
  121. }  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值