1. First query, the worst case is O(n), the best case is O(1), O(n) only needs a linear structure, arrays and linked lists are OK, if you want To achieve O(1), as you said, you must trade space for time, and hash tables are a common practice. In addition, there are some structures with intermediate time complexity. Binary search is a common practice, which can easily accelerate structures that originally require O(n) to O(lgn). Arrays, linked lists, and trees can all be used. Use this approach;
2. Then comes the modification. The modification actually involves querying first and then modifying. The time complexity of the query has already been mentioned. The complexity of the specific data modification depends on what your data is like;
3. Finally, there is sorting. The time complexity of sorting has been said badly. The lower limit of time complexity of the comparison-based sorting algorithm is O(nlgn). There are many structures to achieve it.
Depending on your needs, you want to do quick queries, and then you want to do real-time sorting.
Assuming that your modifications are more than queries, it is recommended that queries do not pursue O(1), and a lower limit is required for sorting. Using a heap or skip list structure can achieve the goal. If your queries are frequent and the modifications are small, you can create a hash table for querying, and you can directly divide the array into two or even use a linked list for sorting.
It feels like we can use trees to solve the problem
1. First query, the worst case is O(n), the best case is O(1), O(n) only needs a linear structure, arrays and linked lists are OK, if you want To achieve O(1), as you said, you must trade space for time, and hash tables are a common practice. In addition, there are some structures with intermediate time complexity. Binary search is a common practice, which can easily accelerate structures that originally require O(n) to O(lgn). Arrays, linked lists, and trees can all be used. Use this approach;
2. Then comes the modification. The modification actually involves querying first and then modifying. The time complexity of the query has already been mentioned. The complexity of the specific data modification depends on what your data is like;
3. Finally, there is sorting. The time complexity of sorting has been said badly. The lower limit of time complexity of the comparison-based sorting algorithm is O(nlgn). There are many structures to achieve it.
Depending on your needs, you want to do quick queries, and then you want to do real-time sorting.
Assuming that your modifications are more than queries, it is recommended that queries do not pursue O(1), and a lower limit is required for sorting. Using a heap or skip list structure can achieve the goal. If your queries are frequent and the modifications are small, you can create a hash table for querying, and you can directly divide the array into two or even use a linked list for sorting.
Std::map in C++ is implemented using red-black trees, which means that the output is sorted by key. Why
If multiple sorting methods are needed, can a std::map be used to save the corresponding reference for each sorting?