C++ - Fastest way to add an item to a sorted array

后端 未结 8 1104
孤城傲影
孤城傲影 2021-01-23 05:18

I\'ve got a database with approximately 200 000 items, which is sorted by username. Now when I add an item to end of array and call my quick sort function to sort that array it

8条回答
  •  清歌不尽
    2021-01-23 05:45

    You can do binary search like this way.. Here You can assume that if val is string type then compare using string comparison function and int AR[] is set of string or You can map them to integer. As the array is sorted , I think binary search will give you the best performance.

    int bsearch(int AR[], int N, int VAL)
    {
        int Mid,Lbound=0,Ubound=N-1;
    
        while(Lbound<=Ubound)
        {
            Mid=(Lbound+Ubound)/2;
            if(VAL>AR[Mid])
                Lbound=Mid+1;
            else if(VAL

提交回复
热议问题