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

后端 未结 8 1174
孤城傲影
孤城傲影 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:55

    Binary search will be of limited interest, as you will need to insert anyway and this will remain a time consuming operation (O(N)). So your first idea of a linear search followed by insertion is good enough; you can combine in a single backward loop. (This is a step of StraightInsertionSort.)

    The truly efficient ways to handle dynamic sorted lists are by maintaining a balanced tree or using a hash table.

提交回复
热议问题