473,396 Members | 2,020 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,396 software developers and data experts.

sort a generic linked list?

I know how to use Icomparable.

However, I can't figure out how to sort a generic linked list? (without
writing the algorithm)

Lets say I have something like this:
class DisJunctiveConstraintList : LinkedList<DisjunctiveConstraint>

class DisjunctiveConstraint { public int Selection ... }
And i want to sort by Selection ?
--
With regards,

Nick
Jan 28 '07 #1
6 6927
>I know how to use Icomparable.
>
However, I can't figure out how to sort a generic linked list? (without
writing the algorithm)
There's no way out using inbuilt API.
You'll need to write your own... or there's a way around:

1. Convert LinkedList into an array.
2. Sort the array
3. Convert the array back to LinkedList

:)
--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
Jan 28 '07 #2

"Gaurav Vaish (www.edujini-labs.com)" <ga*****************@nospam.gmail.com>
wrote in message news:uz**************@TK2MSFTNGP04.phx.gbl...
I know how to use Icomparable.

However, I can't figure out how to sort a generic linked list? (without
writing the algorithm)

There's no way out using inbuilt API.
You'll need to write your own... or there's a way around:

1. Convert LinkedList into an array.
2. Sort the array
3. Convert the array back to LinkedList
That is actually a good approach. Linked lists are inherently not sortable
without a lot of extra work linking and unliking things. Arrays are very
sortable.
Jan 28 '07 #3
Hmm,

Then, I guess I would have to write sth like this:

class ddd {
public int i;
public string name;
public ddd(int i, string name) {
this.i = i;
this.name = name;
}
(... implement Icomparable)
}

LinkedList<ddd a = new LinkedList<ddd >();
a.AddLast( ... a ddd ...);
a.AddLast(... a ddd ...);
a.AddLast(... a ddd ...);
List<ddd b = new List<ddd >();
foreach (ddd item in a) {
b.Add(item);
}
b.Sort();
a.Clear();
foreach (ddd item in b) {
a.AddLast(item);
}
or this:
class ddd {
public int i;
public string name;
public ddd(int i, string name) {
this.i = i;
this.name = name;
}
}

LinkedList<ddda = new LinkedList<ddd>();
a.AddLast( ... a ddd ...);
a.AddLast( ... a ddd ...);
a.AddLast( ... a ddd ...);

SortedList<int, dddb = new SortedList<int, ddd>();
foreach (ddd item in a)
b.Add(item.i, item);
a.Clear();

foreach (ddd item in b.Values)
a.AddLast(item);
foreach (ddd item in a)
Console.WriteLine(item.i + " " + item.name);

So, just wandering ... Is there a better way to make the conversion
LinkedList -List in order to sort it?

Are the two ways above equivalent?

The second way should require more memory, but perhaps is faster?
--
With regards,

Nick
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:u5**************@TK2MSFTNGP04.phx.gbl...
>
"Gaurav Vaish (www.edujini-labs.com)"
<ga*****************@nospam.gmail.comwrote in message
news:uz**************@TK2MSFTNGP04.phx.gbl...
>I know how to use Icomparable.

However, I can't figure out how to sort a generic linked list? (without
writing the algorithm)

There's no way out using inbuilt API.
You'll need to write your own... or there's a way around:

1. Convert LinkedList into an array.
2. Sort the array
3. Convert the array back to LinkedList

That is actually a good approach. Linked lists are inherently not
sortable without a lot of extra work linking and unliking things. Arrays
are very sortable.


Jan 28 '07 #4
On Jan 28, 10:22 am, "Michael A. Covington"
<l...@ai.uga.edu.for.addresswrote:
"Gaurav Vaish (www.edujini-labs.com)" <gaurav.vaish.nos...@nospam.gmail.com>
wrote in messagenews:uz**************@TK2MSFTNGP04.phx.gbl. ..
>I know how to use Icomparable.
However, I can't figure out how to sort a generic linked list? (without
writing the algorithm)
There's no way out using inbuilt API.
You'll need to write your own... or there's a way around:
1. Convert LinkedList into an array.
2. Sort the array
3. Convert the array back to LinkedList
That is actually a good approach.
Agreed. So long as the list isn't enormous (tens of thousands of
elements or more), the overhead of switching to an array and then back
again probably won't be too bad.
Linked lists are inherently not sortable without a lot of extra work linking and unliking things.
I disagree. I've written QuickSort for a linked list before and there
was no linking or unlinking involved. In fact, any basic exchange sort
works just great with a linked list. What costs big time is needing to
find a specific index in the list, because you have to search for it.
Most exchange sorts, however, move forward and backward through the
list, which is cheap. Exchanging elements becomes simply swapping
pointers or values, which is also cheap.
Arrays are very sortable.
True. Or, more properly, arrays sort well using a wider variety of
algorithms than do linked lists, because indexing into an array is
cheap.

Jan 28 '07 #5
Michael A. Covington <lo**@ai.uga.edu.for.addresswrote:

<snip>
That is actually a good approach. Linked lists are inherently not sortable
without a lot of extra work linking and unliking things. Arrays are very
sortable.
You need to use a different approach to sorting for a linked list, but
some sort algorithms work surprisingly well.

See
http://www.chiark.greenend.org.uk/~s.../listsort.html
for an example.

--
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
Jan 29 '07 #6
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrites:
Linked lists are inherently not sortable without a lot of extra work
linking and unliking things. Arrays are very sortable.
Well, as pointed out also by Jon Skeet, linked lists can be sorted
using mergesort in worst-case time O(n log n), without extra memory,
and stably (not changing the order of equal elements). In many
respects this provides better functionality than quicksort on arrays.

The C5 collection library for C#/.NET has such an sorting
implementation for linked lists; http://www.itu.dk/research/c5/

Peter
Jan 29 '07 #7

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

Similar topics

10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
3
by: Peter Olcott | last post by:
How does not specify the sort criteria for Generic.List ?? The way that this is done in C++ STL is to implement operator<(), how is this done in C# and DotNet for Generic.List ???
7
by: Zeba | last post by:
Hi, I have to write program in C# to merge sort a linked list (doubly linked). Is it true that the best way to do it is copy it into an array, sort it and then convert back ? I'm new to C#,...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
4
by: Tony Johansson | last post by:
Hello! What actually does this sentence mean? Create a linked-list generic class that enables you to create a chain of different objects types. When I create a linked-list generic class for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.