Find First Free Position in Array in $O(\log n)$ Time Complexity

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Position
Click For Summary

Discussion Overview

The discussion revolves around finding the first free position in an array with a time complexity of $O(\log n)$. Participants explore various implementations and edge cases of a recursive function designed to achieve this goal, as well as considerations for merging two arrays and deleting elements from them.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants suggest using a binary search approach to find the first free position in the array.
  • A proposed function, FreePos, is presented multiple times with various modifications aimed at improving its correctness and efficiency.
  • Participants discuss edge cases for the FreePos function, including scenarios with arrays of different sizes and contents, such as arrays filled with INT_MAX.
  • There are suggestions to simplify the recursive cases by removing certain conditional checks, though concerns are raised about potential segmentation violations.
  • Participants express uncertainty about the handling of specific cases, particularly when the array has only one element.
  • There is a discussion about the implications of returning an index beyond the last valid position in the array when it is completely full.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best implementation of the FreePos function, with multiple competing views on how to handle edge cases and the overall structure of the function. The discussion remains unresolved regarding the optimal approach.

Contextual Notes

Limitations include unresolved mathematical steps in the recursive function, potential issues with array bounds, and varying interpretations of how to handle completely filled arrays.

  • #31
mathmari said:
Should I declare [m]starsystem[/m] outside the if?? (Wondering)

The would help yes.
The life time of an object ends when we get to the matching closing brace. (Nerd)
 
Technology news on Phys.org
  • #32
I like Serena said:
The life time of an object ends when we get to the matching closing brace. (Nerd)

What do you mean?? (Wondering)
 
  • #33
mathmari said:
What do you mean?? (Wondering)

When you have:
Code:
void function()
{
  int a = 2;
  {
    int a = 3;
    printf("%d\n", a);
  }
  printf("%d\n", a);
}
what do you think gets printed? (Wondering)
 
  • #34
I like Serena said:
When you have:
Code:
void function()
{
  int a = 2;
  {
    int a = 3;
    printf("%d\n", a);
  }
  printf("%d\n", a);
}
what do you think gets printed? (Wondering)

It will print:

3
2

right?? (Wondering)
 
  • #35
mathmari said:
It will print:

3
2

right?? (Wondering)

Yep. (Nod)

When "int a = 3" is declared, local stack memory is allocated for it.
Furthermore it "hides" the "a" that was declared earlier.
When the closing brace comes by, the local stack memory is automatically released.
Afterwards, the original "a" becomes visible again. (Nerd)
 
  • #36
I like Serena said:
Yep. (Nod)

When "int a = 3" is declared, local stack memory is allocated for it.
Furthermore it "hides" the "a" that was declared earlier.
When the closing brace comes by, the local stack memory is automatically released.
Afterwards, the original "a" becomes visible again. (Nerd)

Ahaa... Ok! (Muscle)I have also an other question...

When I declare 'starsystem', before the if statement, do I write it as followed??

[m] starsy_t *starsystem;[/m]

Or should I set it equal to [m]NULL[/m] ??

(Wondering)
 
  • #37
mathmari said:
Ahaa... Ok! (Muscle)I have also an other question...

When I declare 'starsystem', before the if statement, do I write it as followed??

[m] starsy_t *starsystem;[/m]

Or should I set it equal to [m]NULL[/m] ??

(Wondering)

It is good practice to always set a pointer to NULL if you're not setting it to any other specific value. (Smirk)
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
12
Views
3K
  • · Replies 98 ·
4
Replies
98
Views
14K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K