Tuesday, February 9, 2010

Algorithms - MergeSort in C#

Hi All,

Today I was working on small algorithm to sort array using MergeSort. I wrote this program in C#. Putting it here in case anybody want to use it feel free to use.

I used Generic methods here so that input can be any type as long as it uses IComparable interface.

public class MergeSortAlgo
{
public void Merge(ref T[] initialArray) where T : IComparable
{
if (initialArray == null || initialArray.Length == 0)
return;

T[] temp = new T[initialArray.Length];

MSort(ref temp, ref initialArray, 0, initialArray.Length - 1);

}

private void MSort(ref T[] temp, ref T[] initialArray, int start, int end) where T:IComparable
{
if (end - start > 0)
{
this.MSort(ref temp, ref initialArray, start, (end + start) / 2);
this.MSort(ref temp, ref initialArray, (end + start) / 2 + 1, end);
this.MergeSort(ref temp, ref initialArray, start, (end + start) / 2 + 1, end);
}
}

private void MergeSort(ref T[] temp, ref T[] initialArray, int start, int mid, int end) where T:IComparable
{
int i = start;
int j = mid;
int pos = start;

while (i != mid || j != end + 1)
{
if(i == mid)
{
for (; j <= end; j++)
{
temp[pos] = initialArray[j];
pos++;
}

break;
}
else if(j == end + 1)
{
for (; i < mid; i++)
{
temp[pos] = initialArray[i];
pos++;
}

break;
}

if (initialArray[i].CompareTo(initialArray[j]) <= 0)
{
temp[pos] = initialArray[i];
pos++;
i++;
}
else if (initialArray[i].CompareTo(initialArray[j]) > 0)
{
temp[pos] = initialArray[j];
pos++;
j++;
}
}

for (int counter = start; counter <= end; counter++)
{
initialArray[counter] = temp[counter];
}
}
}

Sunday, February 7, 2010

SQL Server 2008 Table Variables vs Temporary Tables

I was looking into difference between these. When to use which one. Got some ideas after reading here and there. Thought to compile this so that it might be helpful for others too.

Temporary Tables:

CREATE TABLE #T1(col1 INT NOT NULL);
INSERT INTO #T VAUES(1)
OR
SELECT ID INTO #T FROM dbo.Nums WHERE ..
  1. Temporary Tables are defined using #
  2. They are always stord in tempdb database
  3. Temporary tables take part in transactions
  4. Scope of temporary tables is current and inner queries.
  5. If tables has named constraints then DB never caches these. So try to use contraints like unique etc only for better performance
  6. Good for data set if it is big as SQL run statistics on these to optimize.
  7. Not good for small data set lot of recompile happens

Global Temporary variables:

  1. Defined using ##
  2. Have global scope
  3. Once nobody is using these SQL server cleans from tempDB.

Table Vairable:


DECLARE @t TABLE(ID INT, PERSON NVARCHAR(200) NOT null)
Select * from @t
Insert into @t Values(...)

  1. SQL doesn't compute statistics so generally slower if data is too big.
  2. Less recompiles so good if data is small.
  3. Stored in tempdb database
  4. Table definition cannot be changed once declared
  5. Doesn't take part in outetransactions
  6. Explicit indexes cannot be created for these.
  7. Only accessible in current contxt. Not in outer or inner queries.
  8. For Table Variables NSERT SELECT happens with minimal logging in SQL 2008.


Wednesday, January 27, 2010

Gmail security issue

Security issues in online free world. Are free things really free.

Today I found a major security issue while troubeshooting a gmail issue.
Issue was from last couple of days my brother was getting someone's else emails and the other guy was getting his. All of my brother's private emails and bank emails are freely going on someone else account. I checked all the settings in gmail if auto forward is setup my mistake or something. But nothing like that was set. Problem is they dont even know each other. I got really frustrated seeing this major security issue in gmail. I tried to contact google support but couldn't found any link, phone number. So I mentiomed about this problem in their forum. Still waiting for somebody to respond. Now I am really really scared if this is the way our online privacy is, in online world.

What should I do now????

This is really a major pain. Hoping somehow gmail team look at this post and fix their problem.

Tuesday, January 26, 2010