Skip to content

Implement search insert position in cairo#221

Merged
coxmars merged 1 commit intoKaizenode:mainfrom
GideonBature:search_insert_pos
May 5, 2025
Merged

Implement search insert position in cairo#221
coxmars merged 1 commit intoKaizenode:mainfrom
GideonBature:search_insert_pos

Conversation

@GideonBature
Copy link
Contributor

This PR adds the implementation of the "Search Insert Position" algorithm in Cairo as requested in issue #215. The algorithm finds the index of a target value in a sorted array, or the position where it would be inserted if not found, with O(log n) time complexity using binary search.

Changes

  • Created a new Scarb project in examples/cairo/scripts/search_insert_position/
  • Implemented the search_insert_pos function with binary search approach
  • Added comprehensive test cases covering all required scenarios:
    • Target found in the array
    • Target not found but belongs in the middle
    • Target smaller than all elements
    • Target larger than all elements
    • Empty array
    • Single element arrays
    • Large arrays
    • Edge cases with u32::MIN and u32::MAX values

Implementation Details

  • The solution uses an efficient binary search algorithm to achieve O(log n) time complexity
  • Handles all edge cases including empty arrays
  • Passes all test cases including the examples mentioned in the issue

Testing

All test cases pass successfully, demonstrating the correctness of the implementation for various scenarios.

Closes #215

@GideonBature
Copy link
Contributor Author

@coxmars This PR is ready for review and to be merged.

Copy link
Contributor

@coxmars coxmars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GideonBature can you add the .tool-versions file please?

@GideonBature
Copy link
Contributor Author

@GideonBature can you add the .tool-versions file please?

sure! I thought I added it, seems I missed that.

@coxmars coxmars merged commit b36a241 into Kaizenode:main May 5, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT]: Search Insert Position Implementation in Cairo

2 participants