Is an ArrayList or a LinkedList better for sorting?

后端 未结 5 1134
情深已故
情深已故 2020-12-10 11:13

I want to use data structure that needs to be sorted every now and again. The size of the data structure will hardly exceed 1000 items.

Which one is better - A

5条回答
  •  旧时难觅i
    2020-12-10 11:51

    If you are just sorting and not dynamically updating your sorted list, then either is fine and an array will be more memory efficient. Linked lists are really better if you want to maintain a sorted list. Inserting an object is fast into the middle of a linked list, but slow into an array.

    Arrays are better if you want to find an object in the middle. With an array, you can do a binary sort and find if a member is in the list in O(logN) time. With a linked list, you need to walk the entire list which is very slow.

    I guess which is better for your application depends on what you want to do with the list after it is sorted.

提交回复
热议问题