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

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

    Easy , direct method cause binary searching is too mainstream. Just need a few lines:

    int where_to_add(int array[], int element)
    {
        int i;
        for (i = length; i >= 0 && array[i-1] > element; i--);
        return i;
    }
    

    Let me know if this is the answer you were looking for

提交回复
热议问题