Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:46:40+00:00 2026-05-30T16:46:40+00:00

Which is better? According to Savitch, each recurse is saved on the stack in

  • 0

Which is better?

According to Savitch, each recurse is saved on the stack in the form of an activation frame. This has overhead. However it takes a few less lines of code to write a recursive version. For an interview which one is better to turn in. The code for both is below.

#include <iostream>

using namespace std;
const int SIZE = 10;
int array[ SIZE ] = { 0,1,2,3,4,5,6,7,8,9 };
int answer = NULL;

void binary_search_recursive( int array[], int start, int end, int value, int& answer )
{
    int mid = (start + end ) / 2;

    if ( array[ mid ] == value )
    {
        answer = mid;
    }
    else if ( array[ mid ] < value )
    {
        binary_search_recursive( array, mid + 1, end, value, answer );
    }
    else
    {
        binary_search_recursive( array, start, mid - 1, value, answer );
    }
}

void binary_search_iterative( int array[], int start, int end, int value, int& answer )
{
    int mid = ( (start + end ) / 2 );

    while( array[ mid ] != value  )
    {
        if ( array[ mid ] < value )
        {
            start = mid; 
            mid = ( ( ( mid + 1 ) + end ) / 2 );
        }
        else
        {
            end = mid;
            mid = ( ( start + ( mid - 1 ) ) / 2 );
        } 
    }
    answer = mid;
}

int main()
{
    binary_search_iterative( array, 0, SIZE - 1, 4, answer);
    cout << answer;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:46:42+00:00Added an answer on May 30, 2026 at 4:46 pm

    For an interview, I’d start by mentioning that both recursive and iterative solutions are possible and similarly trivial to write. Recursive versions have a potential issue with nested stack frames using or even exhausting stack memory (and faulting different pages into cache), but compilers tend to provide tail recursive optimisations that effectively create an iterative implementation. Recursive functions tend to be more self-evidently correct and concise, but aren’t as widely applicable in day to day C++ programming so may be a little less familiar and comfortable to maintenance programmers.

    Unless there’s a reason not to, in a real project I’d use std::binary_search from <algorithm> (http://www.sgi.com/tech/stl/binary_search.html).

    To illustrate tail recursion, your binary_search_recursive algorithm was changed to the assembly below by g++ -O4 -S. Notes:

    • to get an impression of the code, you don’t need to understand every line, but the following helps:
      • movl are move statements (assignments) between registers and memory (the trailing “l” for “long” reflects the number of bits in the registers/memory locations)
      • subl, shrl, sarl, cmpl are subtraction, shift right, shift arithmetic right, and compare instructions, and the important thing to note is that as a side effect they set a few flags, such as “equals” if they produce a 0 result, that is consulted by je (jump if equals) and jge (jump if greater or equal), jne jump if not equal.
    • the answer = mid termination condition is handled at L10, while the recursive steps are instead handled by the code at L14 and L4 and may jump back up to L12.

    Here’s the disassembly of your binary_search_recursive function (the name is mangled in C++ style)…

    __Z23binary_search_recursivePiiiiRi:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        subl    $4, %esp
        movl    24(%ebp), %eax
        movl    8(%ebp), %edi
        movl    12(%ebp), %ebx
        movl    16(%ebp), %ecx
        movl    %eax, -16(%ebp)
        movl    20(%ebp), %esi
        .p2align 4,,15
    L12:
        leal    (%ebx,%ecx), %edx
        movl    %edx, %eax
        shrl    $31, %eax
        leal    (%edx,%eax), %eax
        sarl    %eax
        cmpl    %esi, (%edi,%eax,4)
        je      L10
    L14:
        jge     L4
        leal    1(%eax), %ebx
        leal    (%ebx,%ecx), %edx
        movl    %edx, %eax
        shrl    $31, %eax
        leal    (%edx,%eax), %eax
        sarl    %eax
        cmpl    %esi, (%edi,%eax,4)
        jne     L14
    L10:
        movl    -16(%ebp), %ecx
        movl    %eax, (%ecx)
        popl    %eax
        popl    %ebx
        popl    %esi
        popl    %edi
        popl    %ebp
        ret
        .p2align 4,,7
    L4:
        leal    -1(%eax), %ecx
        jmp     L12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which one is more reliable and has better performance? Setting MySQL unique key and
I have a homework and I need to evaluate which approach is better according
Which scheme according to you is a better one in case of matching? Is
According to this answer , it is better to use the $.ajax() function: $('.showprayer').click(
Which is better? Or use and OR mapper with SP's? If you have a
Which is better to use in PHP, a 2D array or a class? I've
Which is better to do client side or server side validation? In our situation
Which is better in general in terms of the ordering? Do you put the
which is better ??? public class Order { private double _price; private double _quantity;
Which constitutes better object oriented design? Class User { id {get;set} } Class Office

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.