Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do SortedList and List use array and why is LinkedList not much used?

In my mind, List is basically implemented using LinkedList, while a normal Array is implemented as contiguous blocks. I always used List because it is in the Generic namespace and because I thought it used dynamic memory allocation - but I was wrong.

Yesterday I saw the implementation of List using Reflector and found it is actually an array of T(T[]). There are lots of Array.Copy around while manipulating each element in the List. For instance, when you use Insert, it will create a new memory and copy all the elements before/after the inserted elements. So it seem to me the use of List is very expensive.

I saw the SortedList as well. I am not sure why a SortedList also implements an array inside it. Don't you think SortedList would be horrible to use an array as you need to sort the list every time a minor manipulation to the List occurs?

I also wonder why List is so popular as most people use it rather than going for LinkedList. Is it only because of the flexibility of the indexer?

like image 472
abhishek Avatar asked Sep 13 '10 19:09

abhishek


People also ask

When should we use LinkedList and when should we use array?

A simple answer to the question can be given using these points: Arrays are to be used when a collection of similar type data elements is required. Whereas, linked list is a collection of mixed type data linked elements known as nodes. In array, one can visit any element in O(1) time.

Why is a list better than a link list?

Linked lists are an ordered collection of objects. So what makes them different from normal lists? Linked lists differ from lists in the way that they store elements in memory. While lists use a contiguous memory block to store references to their data, linked lists store references as part of their own elements.

What is the difference between linked list and list?

lists are stored sequentially in memory. the elements are stored one after the other. they are faster to access, but slower in addition or deletion of elements. linked lists are not stored sequentially in memory.

Why do we use list in C #?

What is List in C#? List<T> class in C# cotains a strongly typed list of objects. It provides functionality to create a list of objects, add list items, search, sort and manipulate list items. In List<T> , T is the type of objects like int, string, or any user-defined object.

Does C# have a linked list?

LinkedList<T> Class is present in System. Collections. Generic namespace.


2 Answers

Yes, SortedList is O(n) for inserts. Use carefully.

The biggest reason is modern computer design. The CPU cache is very important because RAM is so slow. The memory bus design just couldn't keep up with the rapid advances in CPU clock speeds. Making a high frequency digital signal travel more than an inch is very difficult.

An array has unbeatable cache performance, it very likely that the next element is already in the cache when you iterate it. A linked list gives very small odds that this is the case, the next item is essentially at a random address. That's expensive, it stalls the processor, waiting for the RAM to catch up. Can be hundreds of cycles.

like image 53
Hans Passant Avatar answered Sep 28 '22 05:09

Hans Passant


Because most collections don't need insertions into the middle often. But they do need directly access via an indexer.

like image 43
James Curran Avatar answered Sep 28 '22 06:09

James Curran