Instead of searching a list or container by using linear-search algorithm, item-by-item, binary search is considered more efficient in terms of time and space.
Following sample project created by qt creator and contains following files:
1- SearchIntArray.pro
2- main.cpp
SearchIntArray.pro file contains project configuration.
CONFIG += console TEMPLATE = app SOURCES += main.cppmain.cpp file contains main method and indexOfItem function implementations.
#include <stdio.h> int indexOfItem(int pSearchedtem,int* pArray,int pTotalItemCount) { int mLowerBound = 0; int mHigherBound = pTotalItemCount-1; while( mLowerBound<= mHigherBound ) { int midIndex = mLowerBound+(mHigherBound-mLowerBound)/2; if( pSearchedtem < pArray[midIndex] ) mHigherBound = midIndex-1; else if(pSearchedtem>pArray[midIndex]) mLowerBound = midIndex+1; else return midIndex; } return -1; } int main(int argc, char* argv[]) { int mArray[] = {1, 2, 4, 12, 34, 45, 56, 67, 78, 89, 90}; int mLengthOfArray = sizeof(mArray)/sizeof(int); printf("There are %d items in the array\n", mLengthOfArray); printf("Items :\n"); for(int i = 0; i<mLengthOfArray; i++) { if( i!=mLengthOfArray-1 ) printf("%d,", mArray[i]); else printf("%d\n", mArray[i]); } printf("Indexes :\n"); for(int i = 0; i<mLengthOfArray; i++) { if( i!=mLengthOfArray-1 ) printf("%d,", i); else printf("%d\n\n", i); } int mSearchItem = 12; int mIndex = indexOfItem(mSearchItem,mArray,mLengthOfArray); if( mIndex!=-1 ) printf("Item %d is at index %d\n", mSearchItem, mIndex); else printf("Item %d not found in the array\n",mSearchItem); mSearchItem = 89; mIndex = indexOfItem(mSearchItem,mArray,mLengthOfArray); if( mIndex!=-1 ) printf("Item %d is at index %d\n", mSearchItem, mIndex); else printf("Item %d not found in the array\n",mSearchItem); mSearchItem = 91; mIndex = indexOfItem(mSearchItem,mArray,mLengthOfArray); if( mIndex!=-1 ) printf("Item %d is at index %d\n", mSearchItem, mIndex); else printf("Item %d not found in the array\n",mSearchItem); return 0; }indexOfItem function implements binary search algorithm in C programming language iteratively. indexOfItem function takes the item to search for inside the array as one of its incoming parameters and function returns the index of the found item or -1 if not was found.
Binary Search algorithm, as described at http://algs4.cs.princeton.edu/home/ ,
- lowerBound = 0
- higherBound = itemCount-1
- loop until lowerBound is less than or equal to the upper bound
- divides the sorted container into 2 parts at the center, find middle index
- compares the searchKey with the the item which is at the middle index
- if the searchKey is greater than the item which is at the middle index
- then update the lower bound for search with (middle index+1)
- else if the searchKey is lower than the item which is at the middle index
- then update the upper bound for search with (middle index-1)
- else
- then the searchKey is the item which is at the middle index
- end loop
return indexNotFound
main function contains 3 test-cases for binary search algorithm. First 2 searchKeys (12 and 89) are found in the test array whereas the last searchKey (91) is not found in the list.
Console output contains all the printf function results as follows :
No comments:
Post a Comment