Binary Search a List of custom data types to match just one field

前端 未结 4 1688
清酒与你
清酒与你 2021-01-21 15:07

I have a List:

List allStudents = new List(); 

that contains over 94,000 Student objects, where Student is define

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-21 15:33

    With that many entries you would probably be better off using a Dictionary lookup, which would be amortized O(1). Although there could probably be multiple Students with the same surname, so it would have be something like Dictionary>

    Also as Amit pointed out, using a binary search when there are duplicate items can be tricky, because you don't know which index you will get in the series of duplicates. You would have to search to the left and right of the returned index to see if any other matching surnames exist.

提交回复
热议问题