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.Div Rem
int incr = System.Math.Div Rem((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.Compa re(list[j], list[j + incr]) > 0))
{
listSwap(list, j, j + incr);
j -= incr;
}
}
incr = System.Math.Div Rem((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;InOr der:CompareFunc ;
Swap:SwapProc);
var i,j,incr,last:i nteger;
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+inc r) do begin { Short
circuit! }
Swap(j,j+Incr);
dec(j,incr);
End
End;
incr:=longint(i ncr)*10 div 17;
End;
End; 7 8395
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.co m
"Majnu" <sp**@majnu.net > wrote in message
news:59******** *************** ***@posting.goo gle.com... 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.Div Rem int incr = System.Math.Div Rem((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.Compa re(list[j], list[j + incr]) > 0)) { listSwap(list, j, j + incr); j -= incr; } } incr = System.Math.Div Rem((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;InOr der:CompareFunc ; Swap:SwapProc); var i,j,incr,last:i nteger; 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+inc r) do begin { Short circuit! } Swap(j,j+Incr); dec(j,incr); End End; incr:=longint(i ncr)*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.goo gle.com... 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.Div Rem int incr = System.Math.Div Rem((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.Compa re(list[j], list[j + incr]) > 0)) { listSwap(list, j, j + incr); j -= incr; } } incr = System.Math.Div Rem((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;InOr der:CompareFunc ; Swap:SwapProc); var i,j,incr,last:i nteger; 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+inc r) do begin { Short circuit! } Swap(j,j+Incr); dec(j,incr); End End; incr:=longint(i ncr)*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.Div Rem int incr = System.Math.Div Rem((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.co m> 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.c om> wrote in message news:<uP******* *******@tk2msft ngp13.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.co m
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.co m> 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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.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 features to
them. "Of course," I'm redoing them in python - much of the cut&paste
reuse has become common functions, which then get made more robust and
have a common style and are callable from other (python) tools
directly, instead of having to...
|
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 newsgroups I learned that there
is a function in stdlib.h called system.
int system(const char *command);
First question, is the system command ANSI compliant. Because I include
<cstdlib> and write std::system(command.c_str()); it looks like an...
|
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 slash commands.
This file calls an external shell script via the backtick mechanism, and
saves the value into a psql variable, for latter insertion into a table. It
looks something like this:
|
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. When they login, I will
retrieve all the programs they can load. When they click on a program to
load, I want to load the program in the shell program and run it from within
the shell program. I have a split container control on my shell form. In...
|
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 about sorting and started thinking about shell
sort.... and as part of my trying to pythonise my mind and break my
java mindset
So I decided to tackle this old school problem with the python mindset.
| |
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 wrote...
static void sort(int list, int size)
{
for(int gap = size / 2; gap > 0; gap /= 2)
{
for(int c = 0; c < gap; c++)
{
if(list < list)
{
|
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, another inputboxes will appear asking for the items to be sorted...
then the items you inputed will be printed in the 1st listbox..
then when you click the 2nd cmd button the items in the first listbox will appear in the second listbox but they...
|
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...
|
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,...
| |
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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();...
|
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...
| |
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
| |