随机生成1024个数,用指针进行排序,并实现二分查找

题目:随机生成1024个数,用指针进行排序,并实现二分查找,具体实现如下:

[cpp]  view plain copy
  1. #include<stdlib.h>  
  2. #include<stdio.h>  
  3.   
  4. int SortByPtr(int * pInput, int nLen)  
  5. {  
  6.     if (!pInput)  
  7.     {  
  8.         return 0;  
  9.     }  
  10.     int * pCur = pInput;  
  11.     int * pEnd = pInput + nLen;  
  12.     int * pCur2 = NULL;  
  13.     int * pEnd2 = pEnd;  
  14.     int nTemp = 0;  
  15.     for (; pCur < pEnd - 1; pCur++)  
  16.     {  
  17.         for (pCur2 = pEnd2-1; pCur2 > pCur; pCur2--)  
  18.         {  
  19.             if (*pCur2 < *(pCur2 - 1))  
  20.             {  
  21.                 nTemp = *pCur2;  
  22.                 *pCur2 = *(pCur2 - 1);  
  23.                 *(pCur2 - 1) = nTemp;  
  24.             }  
  25.         }  
  26.     }  
  27.     return 1;  
  28. }  
  29.   
  30. int BinSort(int * pInput, int nLow, int nHigh, int nKey, int * pFind)  
  31. {  
  32.     if (!pInput || !pFind)  
  33.     {  
  34.         return 0;  
  35.     }  
  36.   
  37.     *pFind = 0;  
  38.   
  39.     if (nLow > nHigh)  
  40.     {  
  41.         return 1;  
  42.     }  
  43.   
  44.     int nMid = (nHigh + nLow) / 2;  
  45.   
  46.     if (pInput[nMid] == nKey)  
  47.     {  
  48.         *pFind = 1;  
  49.         return 1;  
  50.     }  
  51.     else if (pInput[nMid] > nKey)  
  52.     {  
  53.         return BinSort(pInput, nLow, nMid - 1, nKey, pFind);  
  54.     }  
  55.     else  
  56.     {  
  57.         return BinSort(pInput, nMid + 1, nHigh, nKey, pFind);  
  58.     }  
  59. }  
  60.   
  61. int BinSortPtr(int * pInput, int nLow, int nHigh, int nKey, int * pFind)  
  62. {  
  63.     if (!pInput || !pFind)  
  64.     {  
  65.         return 0;  
  66.     }  
  67.   
  68.     *pFind = 0;  
  69.   
  70.     if (nLow > nHigh)  
  71.     {  
  72.         return 1;  
  73.     }  
  74.   
  75.     int nMid = (nHigh + nLow) / 2;  
  76.   
  77.     int * pTemp = pInput + nMid;  
  78.   
  79.     if (*pTemp == nKey)  
  80.     {  
  81.         *pFind = 1;  
  82.         return 1;  
  83.     }  
  84.     else if (*pTemp > nKey)  
  85.     {  
  86.         return BinSort(pInput, nLow, nMid - 1, nKey, pFind);  
  87.     }  
  88.     else  
  89.     {  
  90.         return BinSort(pInput, nMid + 1, nHigh, nKey, pFind);  
  91.     }  
  92. }  
  93. int main()  
  94. {  
  95.     int data[10] = { 4, 5, 1, 3, 2, 0, 6, 7, 9, 8 };  
  96.     int i = 0;  
  97.     if (SortByPtr(data, 10) == 0)  
  98.     {  
  99.         printf("排序失败.\n");  
  100.     }  
  101.     else  
  102.     {  
  103.         printf("排序后:\n");  
  104.         for (i = 0; i < 10; i++)  
  105.         {  
  106.             printf("%d  ", data[i]);  
  107.         }  
  108.         printf("\n");  
  109.     }  
  110.     int nFind = 0;  
  111.     if (BinSort(data, 0, 9, 9, &nFind) == 0)  
  112.     {  
  113.         printf("查找失败.\n");  
  114.     }  
  115.     else  
  116.     {  
  117.         if (nFind == 1)  
  118.         {  
  119.             printf("查找成功.\n");  
  120.         }  
  121.         else  
  122.         {  
  123.             printf("查找不成功.\n");  
  124.         }  
  125.     }  
  126.     if (BinSortPtr(data, 0, 9, 9, &nFind) == 0)  
  127.     {  
  128.         printf("查找失败.\n");  
  129.     }  
  130.     else  
  131.     {  
  132.         if (nFind == 1)  
  133.         {  
  134.             printf("查找成功.\n");  
  135.         }  
  136.         else  
  137.         {  
  138.             printf("查找不成功.\n");  
  139.         }  
  140.     }  
  141.     system("pause");  
  142.     return 0;  
  143. }  
运行效果如图1所示:

图1 运行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值