473,233 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

Sorting in LinkedList<>

Hi.

Mostly I program in C++, and I'm not fluent in C# and .NET. In my last
project I began to use LinkedList<and suddenly noticed that can't
find a way to sort it. Does it mean I must implement sorting for
LinkedList<myself?

Thanks in advance

Martin

Dec 14 '06 #1
20 20826

"martin-g" <ma******@mail.ruwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
Hi.

Mostly I program in C++, and I'm not fluent in C# and .NET. In my last
project I began to use LinkedList<and suddenly noticed that can't
find a way to sort it. Does it mean I must implement sorting for
LinkedList<myself?

Thanks in advance

Martin
So it would appear.
Perhaps this article would be helpful to you:

http://msdn2.microsoft.com/en-us/library/6tc79sx1.aspx
Dec 14 '06 #2
pvdg42 wrote:
So it would appear.
Perhaps this article would be helpful to you:

http://msdn2.microsoft.com/en-us/library/6tc79sx1.aspx
Thank you for answer. That's really wild... Looking through the article
I couldn't eventually select the collection class of my need. On the
one hand I want an item to be added to the container at constant time
at any position, so must use LinkeList<>. On the other hand I want to
sort the container, so I must use List<>. FCL has really strange
container organization... Only now I understood: I love STL very much...

Dec 14 '06 #3
martin-g wrote:
pvdg42 wrote:
So it would appear.
Perhaps this article would be helpful to you:

http://msdn2.microsoft.com/en-us/library/6tc79sx1.aspx

Thank you for answer. That's really wild... Looking through the article
I couldn't eventually select the collection class of my need. On the
one hand I want an item to be added to the container at constant time
at any position, so must use LinkeList<>. On the other hand I want to
sort the container, so I must use List<>. FCL has really strange
container organization... Only now I understood: I love STL very much...
So you want constant time insertions and the ability to sort the
collection. The List<collection sounds like a good match.
Insertions are usually O(1) and you can call the Sort method to sort
the collection. Have you looked at SortedDictionary yet? Insertions
are a bit slower, but the items are always stored in order so there's
no need to perform a separate sort operation.

Brian

Dec 14 '06 #4
Brian Gideon <br*********@yahoo.comwrote:
Thank you for answer. That's really wild... Looking through the article
I couldn't eventually select the collection class of my need. On the
one hand I want an item to be added to the container at constant time
at any position, so must use LinkeList<>. On the other hand I want to
sort the container, so I must use List<>. FCL has really strange
container organization... Only now I understood: I love STL very much...

So you want constant time insertions and the ability to sort the
collection. The List<collection sounds like a good match.
Insertions are usually O(1) and you can call the Sort method to sort
the collection. Have you looked at SortedDictionary yet? Insertions
are a bit slower, but the items are always stored in order so there's
no need to perform a separate sort operation.
Insertions into a List<are certainly *not* O(1) in general. When
adding to the end of the list, the additions will be cheap if there's
enough room in the buffer. Otherwise, it requires a copy of all the
items after the new entry (or all the items if a new buffer is
required).

The MSDN docs say it's an O(n) operation too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 14 '06 #5
Jon wrote:
Brian Gideon <br*********@yahoo.comwrote:
Thank you for answer. That's really wild... Looking through the article
I couldn't eventually select the collection class of my need. On the
one hand I want an item to be added to the container at constant time
at any position, so must use LinkeList<>. On the other hand I want to
sort the container, so I must use List<>. FCL has really strange
container organization... Only now I understood: I love STL very much...
So you want constant time insertions and the ability to sort the
collection. The List<collection sounds like a good match.
Insertions are usually O(1) and you can call the Sort method to sort
the collection. Have you looked at SortedDictionary yet? Insertions
are a bit slower, but the items are always stored in order so there's
no need to perform a separate sort operation.

Insertions into a List<are certainly *not* O(1) in general. When
adding to the end of the list, the additions will be cheap if there's
enough room in the buffer. Otherwise, it requires a copy of all the
items after the new entry (or all the items if a new buffer is
required).

The MSDN docs say it's an O(n) operation too.
Yeah, wow, I'm not sure what I was thinking when I wrote that. In
fact, my whole post doesn't make any since...beginning, middle, and
end. That's one of those you just wish you could take back. Ya think
I can talk Google into deleting that one :)

Dec 15 '06 #6

Jon wrote:
Insertions into a List<are certainly *not* O(1) in general. When
adding to the end of the list, the additions will be cheap if there's
enough room in the buffer. Otherwise, it requires a copy of all the
items after the new entry (or all the items if a new buffer is
required).

The MSDN docs say it's an O(n) operation too.
So, List<isn't a linked list, it's an array...? I suppose it would
have to be, if it's meant to be the "first choice" collection for
general use.

Does Framework 2.0 even provide a true link list? Dumb question, I
know, but we're still on 1.1.... :-)

Dec 15 '06 #7
"Bruce Wood" <br*******@canada.comwrote:
>Does Framework 2.0 even provide a true link list?
LinkedList<T>, which was already mentioned earlier in this thread...
Dec 15 '06 #8
Bruce Wood wrote:
So, List<isn't a linked list, it's an array...? I suppose it would
have to be, if it's meant to be the "first choice" collection for
general use.
Yes, List<is based on array.
Does Framework 2.0 even provide a true link list? Dumb question, I
know, but we're still on 1.1.... :-)
I was on 1.1 too, and decided to turn to 2.0 only for its LinkedList<>
generic collection. And some time later realized it's not the linked
list of my dreams.

Dec 15 '06 #9
Bruce Wood wrote:
Jon wrote:
Insertions into a List<are certainly *not* O(1) in general. When
adding to the end of the list, the additions will be cheap if there's
enough room in the buffer. Otherwise, it requires a copy of all the
items after the new entry (or all the items if a new buffer is
required).

The MSDN docs say it's an O(n) operation too.

So, List<isn't a linked list, it's an array...? I suppose it would
have to be, if it's meant to be the "first choice" collection for
general use.
Yes.
Does Framework 2.0 even provide a true link list? Dumb question, I
know, but we're still on 1.1.... :-)
Well, System.Collections.LinkedList still exists, and there's a generic
LinkedList as well.

Note that linked lists don't lend themselves particularly well to
efficient sorting, as sorting ideally requires efficient access to
arbitrary elements of the list.

Jon

Dec 15 '06 #10
Jon Skeet [C# MVP] wrote:
Well, System.Collections.LinkedList still exists, and there's a generic
LinkedList as well.
I do apologise - of course there isn't a System.Collections.LinkedList,
I don't know what I was thinking of...

Jon

Dec 15 '06 #11

Isn't sorting basically looping and then swapping individual elements,
not really arbitrary access to somewhere in the list? A double-linked
list is just fine for sorting 'cause it's easy enough to change the
references in the nodes and swap items. Not as easy as swapping items
in an array, but not a huge problem either.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


On 15 Dec 2006 00:21:50 -0800, "Jon Skeet [C# MVP]" <sk***@pobox.com>
wrote:
>Bruce Wood wrote:
>Jon wrote:
Insertions into a List<are certainly *not* O(1) in general. When
adding to the end of the list, the additions will be cheap if there's
enough room in the buffer. Otherwise, it requires a copy of all the
items after the new entry (or all the items if a new buffer is
required).

The MSDN docs say it's an O(n) operation too.

So, List<isn't a linked list, it's an array...? I suppose it would
have to be, if it's meant to be the "first choice" collection for
general use.

Yes.
>Does Framework 2.0 even provide a true link list? Dumb question, I
know, but we're still on 1.1.... :-)

Well, System.Collections.LinkedList still exists, and there's a generic
LinkedList as well.

Note that linked lists don't lend themselves particularly well to
efficient sorting, as sorting ideally requires efficient access to
arbitrary elements of the list.

Jon
Dec 15 '06 #12
Samuel R. Neff wrote:
Isn't sorting basically looping and then swapping individual elements,
not really arbitrary access to somewhere in the list? A double-linked
list is just fine for sorting 'cause it's easy enough to change the
references in the nodes and swap items. Not as easy as swapping items
in an array, but not a huge problem either.
Well, consider quick sort - it starts off each iteration by picking a
pivot, often by taking the half-way point within the list/sublist. That
requires an O(n) operation in a linked list. I haven't examined the
other types of sort in detail, but I suspect they would have similar
problems.

Something like a bubble sort is very easy to achieve in a linked list,
but I suspect that most of the other sorts are at least tricky to
achieve efficiently. Put it this way, you certainly wouldn't want to
use the same code to sort both types of list, even if they used the
same algorithm.

Having said all this, I did a quick search for sorting linked lists,
and a very reliable source (a friend from university :) has written up
applying mergesort to linked lists - apparently it works well:
http://www.chiark.greenend.org.uk/~s.../listsort.html

Interestingly, wikipedia has this to say (in its Mergesort page):

<quote>
Merge sort is often the best choice for sorting a linked list: in this
situation it is relatively easy to implement a merge sort in such a way
that it requires only T(1) extra space, and the slow random-access
performance of a linked list makes some other algorithms (such as
quicksort) perform poorly, and others (such as heapsort) completely
impossible.
</quote>

So it looks like my original assessment was half-right: you basically
need to treat sorting linked lists differently, and shouldn't expect
the "normal" algorithms to do well, but it's doable. It's worth noting
that the Java "I sort any list" method
java.util.Collections.sort(List<T>) achieves n log(n) performance by
dumping the list into an array, performing a merge sort on it, then
resetting the contents of the original list.

Jon

Dec 15 '06 #13

Samuel R. Neff wrote:
Isn't sorting basically looping and then swapping individual elements,
not really arbitrary access to somewhere in the list? A double-linked
list is just fine for sorting 'cause it's easy enough to change the
references in the nodes and swap items. Not as easy as swapping items
in an array, but not a huge problem either.

Sam
Sam,

If the topic interests you then it's worth taking a look at skip lists.
A skip list is a linked list that maintains a sorted order. By using
them you can take a randomly order linked list and convert it into
sorted skip list in amortized O(n*log(n)) time which is on par with the
quicksort. It requires more memory than a single linked list, but less
than a double-linked list. What I find particulary fascinating is that
most implementations are non-deterministic because they use a random
number generator. There are several properties that make them great
candidates for priority queues as well.

Brian

Dec 15 '06 #14

Jon Skeet [C# MVP] wrote:
Samuel R. Neff wrote:
Isn't sorting basically looping and then swapping individual elements,
not really arbitrary access to somewhere in the list? A double-linked
list is just fine for sorting 'cause it's easy enough to change the
references in the nodes and swap items. Not as easy as swapping items
in an array, but not a huge problem either.

Well, consider quick sort - it starts off each iteration by picking a
pivot, often by taking the half-way point within the list/sublist. That
requires an O(n) operation in a linked list. I haven't examined the
other types of sort in detail, but I suspect they would have similar
problems.
I've written quick sort for a linked list. The only costly operation is
finding the pivot element. After that it's pure sequential access (next
/ previous) to do the swaps. As I said, it's not that bad, but then it
will never be as fast as for an arbitrarily addressable object like an
array.

Dec 15 '06 #15

This from the same person that originally said insertions to List were
O(1). I think you've redeemed yourself. :-)

Unless there is a huge performance difference and sorting is something
that's done quite often, I personally would prefer not to write a
custom collection when writing a custom sorter for an existing
collection would suffice.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 15 Dec 2006 08:37:11 -0800, "Brian Gideon" <br*********@yahoo.com>
wrote:
>
Sam,

If the topic interests you then it's worth taking a look at skip lists.
A skip list is a linked list that maintains a sorted order. By using
them you can take a randomly order linked list and convert it into
sorted skip list in amortized O(n*log(n)) time which is on par with the
quicksort. It requires more memory than a single linked list, but less
than a double-linked list. What I find particulary fascinating is that
most implementations are non-deterministic because they use a random
number generator. There are several properties that make them great
candidates for priority queues as well.

Brian
Dec 15 '06 #16

Samuel R. Neff wrote:
Isn't sorting basically looping and then swapping individual elements,
not really arbitrary access to somewhere in the list? A double-linked
list is just fine for sorting 'cause it's easy enough to change the
references in the nodes and swap items. Not as easy as swapping items
in an array, but not a huge problem either.
Samuel,

If the topic interests you then it's worth taking a look at skip lists.
A skip list is a linked list that maintains a sorted order. By using
them you can take a randomly order linked list and convert it into
sorted skip list in amortized O(n*log(n)) time which is on par with the
quicksort. It requires more memory than a single linked list, but less
than a double-linked list. What I find particulary fascinating is that
most implementations are non-deterministic because they use a random
number generator. There are several properties that make them great
candidates for priority queues as well.

Brian

Dec 15 '06 #17

Samuel R. Neff wrote:
This from the same person that originally said insertions to List were
O(1). I think you've redeemed yourself. :-)
Yeah, that was pretty embarrassing. Afterall, even Susie grade
schooler knows that a List usually has O(n) insertions :)
Unless there is a huge performance difference and sorting is something
that's done quite often, I personally would prefer not to write a
custom collection when writing a custom sorter for an existing
collection would suffice.
I don't think anyone would argue with that.
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
Based on recent performance I shouldn't send you my resume huh? :)

Dec 15 '06 #18

Hi Martin,
Mostly I program in C++, and I'm not fluent in C# and .NET. In my last
project I began to use LinkedList<and suddenly noticed that can't
find a way to sort it. Does it mean I must implement sorting for
LinkedList<myself?
Maybe you should look at this collections:
http://wintellect.com/PowerCollections.aspx (they are written on
Microsoft's demand)

Dec 15 '06 #19

PowerCollections has a Linked List implementation with Sort
functionality? I didn't see it in the docs...

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 15 Dec 2006 11:37:06 -0800, yu******@gmail.com wrote:
>
Hi Martin,
>Mostly I program in C++, and I'm not fluent in C# and .NET. In my last
project I began to use LinkedList<and suddenly noticed that can't
find a way to sort it. Does it mean I must implement sorting for
LinkedList<myself?

Maybe you should look at this collections:
http://wintellect.com/PowerCollections.aspx (they are written on
Microsoft's demand)
Dec 15 '06 #20
Samuel R. Neff <sa********@nomail.comwrites:
PowerCollections has a Linked List implementation with Sort
functionality? I didn't see it in the docs...
C5 collections has linked lists with O(n log(n)) mergesort ... and
much else besides, at http://www.itu.dk/research/c5/

Peter
Dec 17 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sparko | last post by:
My Xml is as follows; <root> <st id="1"> <rt id="1"> <cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs> <ct some attribs> <te some attribs /> <te some attribs />
7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
0
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
3
by: Jon Slaughter | last post by:
When I create a LinkedList<what is a LinkedListNode compared to a value? AddFirst(54) for example, does it turn 54 into a node or what? I'm trying ot find out what the LinkedListNode is...
5
by: Alberto Bencivenni | last post by:
Hi All, Is there a reason why it is not possible to serialize a geenric LinkedList<Tobject containing a custom class marke serializable? Thanks, Alberto
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.