Re: Shell sort for c#

From: Mike Kitchen (publicjoe_at_hotmail.com)
Date: 08/05/04


Date: Thu, 5 Aug 2004 07:41:49 +0100

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" <spam@majnu.net> wrote in message
news:5906a995.0408041228.4afa3822@posting.google.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.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;



Relevant Pages

  • Re: IComparable sort problem
    ... int IComparable.CompareTo ... Because if you were attempting to sort a list of points in ascending ... It would be helpful if you provided a complete IComparer ... generally on larger lists of points. ...
    (microsoft.public.dotnet.languages.csharp)
  • Panic, a strange behavior of lisp program
    ... when SORT is called with a parameter that includes some that ... No actually although CLtL ... this apparent mistake in conversion from CLtL to ANSI-CL. ... Web page that lists all such "obvious" mistakes. ...
    (comp.lang.lisp)
  • Re: Shell sort for c#
    ... > 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). ... > public static void shellSort(IList list, int first, int n, IComparer> comparer) ... > incr:=longint*10 div 17; ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Incident investigation methodologies
    ... However, what sort of reaction ... Speculation gets you nowhere. ... > malware we encounter. ... > of what makes public lists useful - you can get some ...
    (Incidents)
  • Re: Detailed explanation of how a QuickSort Works
    ... Firstly, if you consider the simple "Bubble Sort" algorithm, it works by running through the entire data set, one item at a time, comparing each item to the previous item and swapping them if they are not already in the correct order. ... by simply running through the entire list just once (and splitting it into two smaller lists) you have cut the sorting time in half. ...
    (microsoft.public.vb.general.discussion)