fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int T[5] = {2, 7, 12, 43, 60};
  6.  
  7. /* 索引表 */
  8. int index[3] = {7, 43, 60};
  9.  
  10. int key = 43;
  11. int i, j;
  12. int location = -1;
  13.  
  14. printf("原始数据为:\n");
  15.  
  16. for(i = 0; i < 5; i++)
  17. {
  18. printf("%d ", T[i]);
  19. }
  20.  
  21. printf("\n查找关键字:%d\n", key);
  22.  
  23. /* 查找所在的块 */
  24. for(i = 0; i < 3; i++)
  25. {
  26. if(key <= index[i])
  27. break;
  28. }
  29.  
  30. /* 在对应块中进行顺序查找 */
  31. if(i < 3)
  32. {
  33. for(j = i * 2; j < (i + 1) * 2 && j < 5; j++)
  34. {
  35. if(T[j] == key)
  36. {
  37. location = j + 1;
  38. break;
  39. }
  40. }
  41. }
  42.  
  43. if(location != -1)
  44. printf("查找成功!\n关键字的位置是:%d\n", location);
  45. else
  46. printf("查找失败!\n");
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
原始数据为:
2 7 12 43 60 
查找关键字:43
查找成功!
关键字的位置是:4