how to sort data at the time of adding it, not later?

后端 未结 4 1884
醉话见心
醉话见心 2021-01-21 18:29

I am new to algorithms so please forgive me if this sounds basic or stupid.

I want to know this : instead of adding data into some kind of list and then performing a sor

4条回答
  •  无人共我
    2021-01-21 18:44

    Yes but that's usually slower than adding all the data and sorting it afterwards because to insert the data as it is added, you need to traverse the list every time you add an element.

    With binary search, you need not look at every element but even then, you often need to get more elements from the list as when you sort afterwards.

    So from a performance view, sorted insert is inferior to post sorting.

提交回复
热议问题