473,786 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<myse lf?

Thanks in advance

Martin

Dec 14 '06
20 20916
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.Collecti ons.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 #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.co m>
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.Collecti ons.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.Colle ctions.sort(Lis t<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*********@ya hoo.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<myse lf?
Maybe you should look at this collections:
http://wintellect.com/PowerCollections.aspx (they are written on
Microsoft's demand)

Dec 15 '06 #19

PowerCollection s 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<mys elf?

Maybe you should look at this collections:
http://wintellect.com/PowerCollections.aspx (they are written on
Microsoft's demand)
Dec 15 '06 #20

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

Similar topics

0
1687
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
3127
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<> a superior alternative. Why factors am I not considering here? Why in the general case would std::vector<> be best? Thanks, Dave
3
2744
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 reports at the plan (overall totals), department and division levels which have sorting and grouping implemented with this new
0
1753
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 traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
44
39187
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 List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
35
5895
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 list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
3
3641
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 specifically for? Basically just a wrapper around the reference type?
5
4429
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.