Monday, 19 October 2009

How to implement a merge sort on a Doubly Linked List using C#

I have been working on a custom linked list recently and required a sorting algorithm to sort the doubly linked list.

Mergesort works better on linked lists than it does on arrays. It avoids the need for the auxiliary space, and becomes a simple, reliably O(N log N) sorting algorithm. And as an added bonus, it's stable too.

Here is a quick merge sort algorithm in C#.Net 3.5 (Note - T is my custom node interface) :

private T Merge_Sort(T first)

{

if (first == null || first.Next == null)

return first;


T second = Split(first);

first = Merge_Sort(first);

second = Merge_Sort(second);

return Merge(first, second);

}

private T Split(T item)

{

if (item == null || item.Next == null)

return null;

T second = item.Next;

item.Next = second.Next;


second.Next = Split(second.Next);

return second;

}

private T Merge(T first,

T second)

{

if (first == null)

return second;


if (second == null)

return first;

// If both are not null, we will run the comparer

// If first is less than second

if (first.CompareTo(second) < 0)

{

first.Next = Merge(first.Next, second);

first.Next.Previous = first;

first.Previous = default(T);


return first;

}

else

{

second.Next = Merge(first, second.Next);

second.Next.Previous = second;

second.Previous = default(T);

return second;

}

}

For the full implementation of the Custom Doubly Linked List, look at this post.

Wednesday, 19 August 2009

Native VHD Boot for Windows 7

With Windows 7 and Windows Server 2008 R2, you can configure a VHD(virtual hard disk) file and boot from it, without the need to repartition your existing drive.

i) Native boot means you can fully utilize all hardwares connected to the system.
ii) You cannot use BitLocker when booting from VHD.
iii) Hibernate is not supported when booting from VHD.
iv) Native boot supports all 3 types of VHD files : Fixed, Dynamic and Differencing Disk. Note: Should you not have enough physical storage space as compared to the size of the VHD file, you might get a BSOD.

Simplified instructions for booting from VHD.
1) Start Windows 7 DVD Installation.
2) Press Shift+F10 for the command prompt.
3) Type "diskpart"
4) Type "create vdisk file=Path\Filename.vhd type=fixed maximum=xxxxxx". eg. (create vdisk file=D:\Win7.vhd type=fixed maximum=100000)<-- 100000 Megabytes, type can be dynamic, fixed, etc...
5) Type "select vdisk file=Path\Filename.vhd" to select the created vhd.
6) Type "attach vdisk", and you should get a confirmation that it is attached.
7) Type "exit" and continue installation, and select the attached vhd drive as the installation drive.
8) Boot and it should work.

Alternatively, you can use the disk management tools to create a vhd in Windows 7 via the Windows 7 GUI should you require another vhd copy.

How to remove vhd file.
1) Run elevated command prompt and type "bcdedit /delete {guid} /cleanup"

Saturday, 25 July 2009

Windows 7 RTM

Windows 7 and Windows 2008 R2 has been RTM. Here are the relevant dates:

* Two days after official RTM: OEMs
* August 6: Downloadable for ISVs, IHV's, TechNet subscribers, MSDN subscribers
* August 7: Downloadable for Volume Licence with Software Assurance (English only)
* August 16: Downloadable for Partner Program Gold / Certified members (English-only)
* August 23: Downloadable for Action Pack subscribers (English Only)
* September 1: Purchasing for Volume License without Software Assurance (no mention of specific language availability)
* By October 1: Downloadable for Partner Program Gold / Certified members, Action Pack subscribers, TechNet subscribers, MSDN subscribers (remaining languages)
* October 22: Purchasing at retail locations

Friday, 24 July 2009

Concatenating multiple rows as a single column in a row in SQL Server 2005/2008 (TSQL)

Assuming we have a one to many table, and we need to list all children items per parent in a single row, here is a quick way of doing it in SQL Server 2005/2008 (TSQL)

The following will concatenate all children names belonging to the parent with a ',' delimiter.

 SELECT DISTINCT parent.id, parent.name, CA, CB
FROM ParentTable parent
CROSS APPLY
(
SELECT child.name + ','
FROM ChildA child
WHERE child.parentId = parent.id
ORDER BY child.name
FOR XML PATH('')
) childA(CA)
CROSS APPLY
(
SELECT child2.name + ','
FROM ChildB child2
WHERE child2.parentId = parent.id
ORDER BY child2.name
FOR XML PATH('')
) childB(CB)

Monday, 6 July 2009

IE 8 $10K Challenge

Microsoft is hosting an online treasure hunt (Started 19th June 09), and the first person to find the hidden webpage using the clues provided from time to time will be awarded with $10,000 dollars! But the catch is, you have to use IE 8 to access some of the clues.

Check it out here

Good Luck and happy hunting!

Parameter constraint on dynamic type

It can be accomplished by using the new() keyword.
"new()" indicates the Type T's constructor must not contain any parameters.

Example:

public class A<T> where T : B, new()

Hierarchy Table SQL Statement using CTE (Common Table Expressions)

Assuming a table with fields id, name, parent_id where parent_id is self referencing.

We can use the T-SQL Common Table Expression to easily loop through all children items.

Example Table:
(categories)
id | name | parent_id
1 | CatA | NULL
2 | CatB | 1
3 | CatC | 1
4 | CatD | 2
5 | CatE | 3

 -- This will select all children belonging to CatA where id = 1
;With Hierarchy
As
(
SELECT id, name, parent_id
FROM categories
WHERE id = 1 -- CatA Id
UNION ALL
SELECT child.id, child.name, child.parent_id
FROM categories child
INNER JOIN Hierarchy parent on child.parent_id = parent.id
)
-- This will select all children from the Hierarchy
Select * from Hierarchy