Hi community,
just in case somebody needs a shellsort in c#, I rewrote the pascal
code that I found in another newsgroup. Here are both. For more
explanation on the pascal code you can search for the post on google.
I was interested in the shell sort because I needed to sort long lists
of data that may already be in order. For that reason I didn't want to
use the arraylist's sort method, which uses quick sort (and has a
worst case for lists that are already in order). However, I found out
that the shell sort performs slower than the arraylist's sort method,
even for ordered lists with 1000000 members. I guess this is because
the sort method is hard wired into c#, but I'm not sure.
Anyway, here it is:
public static void shellSort(IList list, int first, int n, IComparer
comparer)
{
int dummyRemainder; // not needed but required by System.Math.DivRem
int incr = System.Math.DivRem((n * 10), 17, out dummyRemainder);
int last = n - 1 + first;
while(incr > 0)
{
for(int i = first; i <= (last - incr); i++)
{
int j = i;
while((j >= first) &&
(comparer.Compare(list[j], list[j + incr]) > 0))
{
listSwap(list, j, j + incr);
j -= incr;
}
}
incr = System.Math.DivRem((incr * 10), 17, out dummyRemainder);
}
}
private static void listSwap(IList list, int i, int j)
{
object o = list[i];
list[i] = list[j];
list[j] = o;
}
Procedure ShellSort(first,n:integer;InOrder:CompareFunc;
Swap:SwapProc);
var i,j,incr,last:integer;
Begin
incr:=longint(n)*10 div 17;
last:=n-1+first;
while incr>0 do begin
for i:=first to last-incr do begin
j:=i;
while (j>=first) and Not InOrder(j,j+incr) do begin { Short
circuit! }
Swap(j,j+Incr);
dec(j,incr);
End
End;
incr:=longint(incr)*10 div 17;
End;
End; 7 8367
Manju,
Your implementation is subject to a lot of boxing, so that could be part
of the perf degredation. You might get better results using a generic
method in .NET 2.0.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Majnu" <sp**@majnu.net> wrote in message
news:59**************************@posting.google.c om... Hi community,
just in case somebody needs a shellsort in c#, I rewrote the pascal code that I found in another newsgroup. Here are both. For more explanation on the pascal code you can search for the post on google.
I was interested in the shell sort because I needed to sort long lists of data that may already be in order. For that reason I didn't want to use the arraylist's sort method, which uses quick sort (and has a worst case for lists that are already in order). However, I found out that the shell sort performs slower than the arraylist's sort method, even for ordered lists with 1000000 members. I guess this is because the sort method is hard wired into c#, but I'm not sure.
Anyway, here it is:
public static void shellSort(IList list, int first, int n, IComparer comparer) { int dummyRemainder; // not needed but required by System.Math.DivRem int incr = System.Math.DivRem((n * 10), 17, out dummyRemainder); int last = n - 1 + first;
while(incr > 0) { for(int i = first; i <= (last - incr); i++) { int j = i; while((j >= first) && (comparer.Compare(list[j], list[j + incr]) > 0)) { listSwap(list, j, j + incr); j -= incr; } } incr = System.Math.DivRem((incr * 10), 17, out dummyRemainder); } }
private static void listSwap(IList list, int i, int j) { object o = list[i]; list[i] = list[j]; list[j] = o; }
Procedure ShellSort(first,n:integer;InOrder:CompareFunc; Swap:SwapProc); var i,j,incr,last:integer; Begin incr:=longint(n)*10 div 17; last:=n-1+first;
while incr>0 do begin for i:=first to last-incr do begin j:=i; while (j>=first) and Not InOrder(j,j+incr) do begin { Short circuit! } Swap(j,j+Incr); dec(j,incr); End End; incr:=longint(incr)*10 div 17; End; End;
Hi Manju
I have a few sort routines on my site that you may wish to look at. These
are tried and tested, and ported from C.
They include a shell sort routine which can be found at the following link http://www.publicjoe.f9.co.uk/csharp/sort07.html
Hope this helps
Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
"Majnu" <sp**@majnu.net> wrote in message
news:59**************************@posting.google.c om... Hi community,
just in case somebody needs a shellsort in c#, I rewrote the pascal code that I found in another newsgroup. Here are both. For more explanation on the pascal code you can search for the post on google.
I was interested in the shell sort because I needed to sort long lists of data that may already be in order. For that reason I didn't want to use the arraylist's sort method, which uses quick sort (and has a worst case for lists that are already in order). However, I found out that the shell sort performs slower than the arraylist's sort method, even for ordered lists with 1000000 members. I guess this is because the sort method is hard wired into c#, but I'm not sure.
Anyway, here it is:
public static void shellSort(IList list, int first, int n, IComparer comparer) { int dummyRemainder; // not needed but required by System.Math.DivRem int incr = System.Math.DivRem((n * 10), 17, out dummyRemainder); int last = n - 1 + first;
while(incr > 0) { for(int i = first; i <= (last - incr); i++) { int j = i; while((j >= first) && (comparer.Compare(list[j], list[j + incr]) > 0)) { listSwap(list, j, j + incr); j -= incr; } } incr = System.Math.DivRem((incr * 10), 17, out dummyRemainder); } }
private static void listSwap(IList list, int i, int j) { object o = list[i]; list[i] = list[j]; list[j] = o; }
Procedure ShellSort(first,n:integer;InOrder:CompareFunc; Swap:SwapProc); var i,j,incr,last:integer; Begin incr:=longint(n)*10 div 17; last:=n-1+first;
while incr>0 do begin for i:=first to last-incr do begin j:=i; while (j>=first) and Not InOrder(j,j+incr) do begin { Short circuit! } Swap(j,j+Incr); dec(j,incr); End End; incr:=longint(incr)*10 div 17; End; End;
Majnu <sp**@majnu.net> wrote: just in case somebody needs a shellsort in c#, I rewrote the pascal code that I found in another newsgroup. Here are both. For more explanation on the pascal code you can search for the post on google.
I was interested in the shell sort because I needed to sort long lists of data that may already be in order. For that reason I didn't want to use the arraylist's sort method, which uses quick sort (and has a worst case for lists that are already in order). However, I found out that the shell sort performs slower than the arraylist's sort method, even for ordered lists with 1000000 members. I guess this is because the sort method is hard wired into c#, but I'm not sure.
The sort method is in no way hard wired into C#.
Anyway, here it is:
public static void shellSort(IList list, int first, int n, IComparer comparer) { int dummyRemainder; // not needed but required by System.Math.DivRem int incr = System.Math.DivRem((n * 10), 17, out dummyRemainder);
If you don't need the remainder, why don't you just use
int incr = (n*10)/17;
That's unlikely to be the bottleneck, but it would be a good starting
point.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks for your input. How would a generic method look like in this
example? I do want to be able to sort ArrayLists of objects.
Greetings,
Majnu
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<uP**************@tk2msftngp13.phx.gbl>... Manju,
Your implementation is subject to a lot of boxing, so that could be part of the perf degredation. You might get better results using a generic method in .NET 2.0.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
Majnu <sp**@majnu.net> wrote: Thanks for your input. How would a generic method look like in this example? I do want to be able to sort ArrayLists of objects.
If they're objects, it should be fine - there'll be no boxing involved.
(Your code itself doesn't do any boxing or unboxing, as far as I can
see.)
However, the fact that you're using IList might be causing performance
problems - ArrayList can just use its own internal array directly. Have
you tried (just for testing purposes) changing your parameter to be
object[] and measuring the performance in that situation?
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks, I will give it a try.
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>... Majnu <sp**@majnu.net> wrote: Thanks for your input. How would a generic method look like in this example? I do want to be able to sort ArrayLists of objects.
If they're objects, it should be fine - there'll be no boxing involved.
(Your code itself doesn't do any boxing or unboxing, as far as I can see.)
However, the fact that you're using IList might be causing performance problems - ArrayList can just use its own internal array directly. Have you tried (just for testing purposes) changing your parameter to be object[] and measuring the performance in that situation? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: eichin |
last post by:
One of my recent projects has involved taking an accretion of sh and
perl scripts and "doing them right" - making them modular, improving
the error reporting, making it easier to add even more...
|
by: Siemel Naran |
last post by:
Hi. I'm writing a command shell that reads commands from standard input.
At this point I have the command in a std::string. Now I want to execute
this command in the shell. From the Borland...
|
by: David Roche |
last post by:
Hello,
I searched all through Google Groups, Google, and the Postgres docs, but
to no avail. I hope someone can help me out here!
I have a file that contains SQL, and some Postgres-specific...
|
by: John Wright |
last post by:
I asked once before and got a good response, but it was not quite what we
needed. I am reposting clarifying my requirements. I want to develop a
shell program that requires the user to login. ...
|
by: akameswaran |
last post by:
Disclaimer - I recognize this is not a practical exercise. There are
many implementations around that would do the job better, more
efficiently (Meaning in C) or whatever.
I caught some thread...
|
by: Dixie |
last post by:
Is there a way to shell to Microsoft Word from Access and load a specific
template - using VBA?
dixie
|
by: Leach0789 |
last post by:
hey guys. I'm new to this forum. I'm taking computer science at my high school this year, and was wondering if you could help me out with my shell sort program im writing. Here's the method that I...
|
by: prassaad |
last post by:
Hi frnds,
I m learning SHELL PROGRAMMING.
It is easy to sort elments in C by using Array.But I dont know
how to work on array in SHELL PROGRAMMING??
I m waiting...........
|
by: anelie |
last post by:
the problem is:
using 2 listboxes and 2 command buttons..
when you click the 1st cmd button an inputbox will appear asking the number of items to be sorted..then when you inputed a number,...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |