图解数据结构(4)——二分法查找法

转载自http://www.cppblog.com/guogangj/archive/2009/10/15/98649.html

如何从数组里找一个元素的位置?如果排列是无序的,我们只能从头到尾找,但如果排列是有序的,我们则可以用别的更好的方法,二分查找法就类似我们在英汉词典里找一个单词的方法。如下图所示(假如我们要查找的数字是“88”):

下面我给出了一段demo代码,来演示二分查找法比顺序查找快多少,代码为了方便起见,初始化有序表的时候填入的数字都是均匀的,而事实上数字可以不均匀。你可以调整一下代码中TABLE_SIZE的值,从500,调到5000,再调到10000,再调到30000……你会发觉两者差距越来越明显。我在第一篇的地方提到二分查找法的复杂度为Ο(logn),而顺序查找的复杂度为Ο(n),当n越来越大时候,Ο(logn)的优势也就越来越明显,当然了,前提是“有序”,才可用二分查找法。

  1. #include "stdio.h"  
  2. #include "time.h"  
  3.   
  4. #define TABLE_SIZE 50000  
  5.   
  6. //returns the position, -1 means failed.  
  7. int SequenceSearch(int *pArray, int iArraySize, int iVal)  
  8. {  
  9.     int i;  
  10.     for(i=0; i<iArraySize; i++)  
  11.     {  
  12.         if(pArray[i]==iVal)  
  13.             return i;  
  14.     }  
  15.   
  16.     return -1;  
  17. }  
  18.   
  19. //returns the position, -1 means failed.  
  20. int BinarySearch(int *pArray, int iArraySize, int iVal)  
  21. {  
  22.     int iLeft = 0;  
  23.     int iRight = iArraySize-1;  
  24.     while(iLeft<=iRight)  
  25.     {  
  26.         int iMiddle = (iLeft+iRight)/2;  
  27.         if(iVal < pArray[iMiddle])  
  28.         {  
  29.             iRight = iMiddle-1;  
  30.         }  
  31.         else if(iVal > pArray[iMiddle])  
  32.         {  
  33.             iLeft = iMiddle+1;  
  34.         }  
  35.         else  
  36.             return iMiddle;  
  37.     }  
  38.   
  39.     return -1;  
  40. }  
  41.   
  42. int main(int argc, char* argv[])  
  43. {  
  44.     //make the table  
  45.     int table[TABLE_SIZE];  
  46.     int i;  
  47.     for(i=0; i<TABLE_SIZE; i++)  
  48.     {  
  49.         table[i] = i*2;  
  50.     }  
  51.   
  52.     clock_t ctBegin = clock();  
  53.     //Test sequence search  
  54.     for(i=0; i<TABLE_SIZE; i++)  
  55.     {  
  56.         SequenceSearch(table, TABLE_SIZE, i*2);  
  57.     }  
  58.     clock_t ctEnd = clock();  
  59.   
  60.     printf("SequenceSearch takes %d clocks.\n", ctEnd-ctBegin);  
  61.   
  62.     //Test binary search  
  63.     ctBegin = clock();  
  64.     for(i=0; i<TABLE_SIZE; i++)  
  65.     {  
  66.         BinarySearch(table, TABLE_SIZE, i*2);  
  67.     }  
  68.     ctEnd = clock();  
  69.       
  70.     printf("BinarySearch takes %d clocks.\n", ctEnd-ctBegin);  
  71.   
  72.     return 0;  
  73. }  



转载自http://www.cppblog.com/guogangj/archive/2009/10/15/98649.html

如何从数组里找一个元素的位置?如果排列是无序的,我们只能从头到尾找,但如果排列是有序的,我们则可以用别的更好的方法,二分查找法就类似我们在英汉词典里找一个单词的方法。如下图所示(假如我们要查找的数字是“88”):

下面我给出了一段demo代码,来演示二分查找法比顺序查找快多少,代码为了方便起见,初始化有序表的时候填入的数字都是均匀的,而事实上数字可以不均匀。你可以调整一下代码中TABLE_SIZE的值,从500,调到5000,再调到10000,再调到30000……你会发觉两者差距越来越明显。我在第一篇的地方提到二分查找法的复杂度为Ο(logn),而顺序查找的复杂度为Ο(n),当n越来越大时候,Ο(logn)的优势也就越来越明显,当然了,前提是“有序”,才可用二分查找法。

  1. #include "stdio.h"  
  2. #include "time.h"  
  3.   
  4. #define TABLE_SIZE 50000  
  5.   
  6. //returns the position, -1 means failed.  
  7. int SequenceSearch(int *pArray, int iArraySize, int iVal)  
  8. {  
  9.     int i;  
  10.     for(i=0; i<iArraySize; i++)  
  11.     {  
  12.         if(pArray[i]==iVal)  
  13.             return i;  
  14.     }  
  15.   
  16.     return -1;  
  17. }  
  18.   
  19. //returns the position, -1 means failed.  
  20. int BinarySearch(int *pArray, int iArraySize, int iVal)  
  21. {  
  22.     int iLeft = 0;  
  23.     int iRight = iArraySize-1;  
  24.     while(iLeft<=iRight)  
  25.     {  
  26.         int iMiddle = (iLeft+iRight)/2;  
  27.         if(iVal < pArray[iMiddle])  
  28.         {  
  29.             iRight = iMiddle-1;  
  30.         }  
  31.         else if(iVal > pArray[iMiddle])  
  32.         {  
  33.             iLeft = iMiddle+1;  
  34.         }  
  35.         else  
  36.             return iMiddle;  
  37.     }  
  38.   
  39.     return -1;  
  40. }  
  41.   
  42. int main(int argc, char* argv[])  
  43. {  
  44.     //make the table  
  45.     int table[TABLE_SIZE];  
  46.     int i;  
  47.     for(i=0; i<TABLE_SIZE; i++)  
  48.     {  
  49.         table[i] = i*2;  
  50.     }  
  51.   
  52.     clock_t ctBegin = clock();  
  53.     //Test sequence search  
  54.     for(i=0; i<TABLE_SIZE; i++)  
  55.     {  
  56.         SequenceSearch(table, TABLE_SIZE, i*2);  
  57.     }  
  58.     clock_t ctEnd = clock();  
  59.   
  60.     printf("SequenceSearch takes %d clocks.\n", ctEnd-ctBegin);  
  61.   
  62.     //Test binary search  
  63.     ctBegin = clock();  
  64.     for(i=0; i<TABLE_SIZE; i++)  
  65.     {  
  66.         BinarySearch(table, TABLE_SIZE, i*2);  
  67.     }  
  68.     ctEnd = clock();  
  69.       
  70.     printf("BinarySearch takes %d clocks.\n", ctEnd-ctBegin);  
  71.   
  72.     return 0;  
  73. }  



猜你喜欢

转载自blog.csdn.net/ytlcainiao/article/details/45915283
今日推荐