C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

バイナリ検索とシーケンシャル検索を比較するC++プログラム


バイナリ検索とシーケンシャルまたはリニア検索はどちらも、コンピュータプログラミングで要素を検索するために使用されます。二分探索の時間計算量はO(log(n))であり、順次探索はO(n)です。

アルゴリズム

Begin
   Algorithm for Binary Search:
   BinarySearch() function with ‘arr’ the array of data and ‘n’ the number of values, start and end index, iteration count and element to be searched in the argument list.
   Increment iteration counter and compare the item value with the a[mid].
   If item < a[mid] choose first half otherwise second half to proceed further.
   Return iteration value on successful search.
End

サンプルコード

#include<iostream>
using namespace std;
int BinarySearch(int a[], int start, int end, int item, int iter) {
   int i, mid;
   cout<<"\niteration "<<iter+1;
   iter++;
   mid = start + (end-start+1)/2;
   if(item > a[end] || item < a[start] || mid == end) {
      cout<<"\nNot found";
      return iter;
   } else if(item == a[mid]) {
      cout<<"\n item found at "<<mid<<" index.";
      return iter;
   } else if(item == a[start]) {
      cout<<"\n item found at "<<start<<" index.";
      return iter;
   } else if(item == a[end]) {
      cout<<"\n item found at "<<end<<" index.";
      return iter;
   } else if(item > a[mid])
         BinarySearch(a, mid, 9, item, iter);
      else
         BinarySearch(a, start, mid, item, iter);
   }
   int LinearSearch(int a[], int n, int item) {
      int i;
      for(i = 0; i < n; i++) {
         cout<<"\niteration "<<i+1;
         if(a[i] == item) {
            cout<<"\n item found at "<<i<<" index.";
         return i+1;
      }
   }
   cout<<"\nNot found";
}
int main() {
   int n, i, B, L, a[10]={2, 7, 14, 24, 26, 35, 38, 41, 49, 53};
   cout<<"\nEnter the element to be searched: ";
   cin>>n;
   cout<<"\n\n\t\t\tBinary Search :";
   B = BinarySearch(a, 0, 9, n, 0);
   cout<<"\n\n\t\t\tLinear Search :";
   L = LinearSearch(a, 10, n);
   if(L > B)
      cout<<"\n\nBinary search is better for this search.";
   else if(L < B)
      cout<<"\n\nLinear search is better for this search.";
   else
      cout<<"\n\nBoth are equally efficient for this search.";
   return 0;
}

出力

Enter the element to be searched: 7
Binary Search :
iteration 1
iteration 2
iteration 3
iteration 4
item found at 1 index.
Linear Search :
iteration 1
iteration 2
item found at 1 index.
Linear search is better for this search.
Enter the element to be searched: 53
Binary Search :
iteration 1
item found at 9 index.
Linear Search :
iteration 1
iteration 2
iteration 3
iteration 4
iteration 5
iteration 6
iteration 7
iteration 8
iteration 9
iteration 10
item found at 9 index.
Binary search is better for this search.
Enter the element to be searched: 1
Binary Search :
iteration 1
Not found
Linear Search :
iteration 1
iteration 2
iteration 3
iteration 4
iteration 5
iteration 6
iteration 7
iteration 8
iteration 9
iteration 10
Not found
Binary search is better for this search.

  1. C ++プログラムでの二分探索?

    二分探索は、半区間探索、対数探索、または二分探索とも呼ばれ、ソートされた配列内のターゲット値の位置を見つける検索アルゴリズムです。二分探索は、ターゲット値を配列の中央の要素と比較します。それらが等しくない場合、ターゲットが存在できない半分が削除され、残りの半分で検索が続行され、再び中央の要素がターゲット値と比較され、ターゲット値が見つかるまでこれが繰り返されます。残りの半分が空の状態で検索が終了した場合、ターゲットは配列に含まれていません。アイデアは単純ですが、バイナリ検索を正しく実装するには、特に配列の値が範囲内の整数のすべてではない場合、終了条件と中間点の計算に関する微妙な点に注意する必要

  2. 二分探索木で左回転を実行するC++プログラム

    二分探索木は、すべてのノードが次の2つのプロパティを持つソートされた二分木です- ノードの右側のサブツリーには、親ノードのキーよりも大きいすべてのキーがあります。 ノードの左側のサブツリーには、親ノードのキーよりも少ないすべてのキーがあります。各ノードには2つ以上の子を含めることはできません。 木の回転は、二分木の要素の順序を妨げることなく構造を変更する操作です。ツリー内で1つのノードを上に移動し、1つのノードを下に移動します。これは、ツリーの形状を変更したり、小さいサブツリーを下に移動したり、大きいサブツリーを上に移動したりして高さを低くしたりするために使用され、多くのツリー操作の