Skip to content

BinarySearch Find Insert Index Optimization #8

@KFKMan

Description

@KFKMan

Current approach is based on non-recurring (non same priority values) values.
For arrays with recurring values (or same priority values) we can optimize FindInsertIndexBS function

#ifdef COMPARE_USE_SIMPLE
        if (comparer(element, GetIndex(arr, mid, elementSize)) == 1) //arr[mid] < element
        #else
        if (comparer(element, GetIndex(arr, mid, elementSize)) > 0)
        #endif
        {
            left = mid + 1;
        }
        else
        {
            right = mid;
        }

As a design comparer function will return 1 (or values bigger than zero if it's not simple comparer) for bigger, 0 for same, -1 (or values lower than zero if it's not simple comparer) for lower priority.

If they are have same priority (0) we can insert element to that index, we don't need to continue.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions