Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Binary Search Algorithm ( Is there anyone need it ?)

Last post 06-20-2008 5:49 AM by Mohammad Rastkar. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-29-2008 7:57 AM

    Binary Search Algorithm ( Is there anyone need it ?)

    #include <iostream>
    #include <string>
    #include "binsearch"
    using namespace BS;

    int _comp(char *a, char *b);

    template<typename T>
    void print_array(T *array, size_t array_size)
    {
     for(int i = 0; i < array_size; ++i)
     {
      std::cout << "   " << array[i] << std::endl;
     }
    }

    void example1();
    void example2();
    void example3();

    int main()
    {
     example3();
     return 0;
    }

    int _comp(char *a, char *b)
    {
     return strcmp(a, b);
    }

    void example1()
    {
     int array[] = {-1, 7, 9, 0, 17, 13, 73};
     int array_size = sizeof(array)/sizeof(array[0]);
     sort<int>(array, array_size, comp);
     std::cout << "Array elements:\n";
     print_array(array, array_size);
     std::cout << "\nBinary Search\n";
     std::cout << "   search: 13\n";
     std::cout << "   search result: " << bin_search<int>(0, array_size, array, 13, comp);
    }

    void example2()
    {
     char array[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
     int array_size = sizeof(array)/sizeof(array[0]);
     std::cout << "Array elements:\n";
     print_array(array, array_size);
     std::cout << "\nBinary Search:\n";
     std::cout << "   enter your search: ";
     char search;
     std::cin >> search;
     std::cout << "   search result: " << bin_search<char>(0, array_size, array, search, comp);
    }

    void example3()
    {
     char *array[] = {"Michelle", "Laetitia", "Cindy", "Christina", "Monica", "Claudia"};
     int array_size = sizeof(array)/sizeof(array[0]);
     std::cout << "\nArray elements \n\nbefore sorting:\n";
     print_array(array, array_size);
     sort(array, array_size, _comp);
     std::cout << "\nafter:\n";
     print_array(array, array_size);
     std::cout << "\nBinary Search\n";
     std::string search = "Claudia";
     std::cout << "   search: " << search;
     std::cout << "\n   search result:= " << bin_search(0, array_size, array, (char*)search.c_str(), _comp);
    }


     

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 06-20-2008 5:49 AM In reply to

    Wink [;)] Not here!

    Hi MazharAliKazi,

    If you want to post a code snippet or article or other things (not a question), please go to your 'Submit Resource' section and create for example a 'Code Sample' and submit your code there.

    - Thanks

    - NetTimeSaver : My software that logs your connecting informations (like duration).
    Filed under: ,
    • Post Points: 5
Page 1 of 1 (2 items)