Sunday, September 30, 2012

Objective C InsertionSort


-(NSMutableArray*)InsertionSort:(NSMutableArray*)array
{
    for(int j=1;j< [array count];j++)
    {
        id key = [array objectAtIndex:j];
            int i =j-1;
     
       while (i>-1 && [array objectAtIndex:i] > key) {
                [array replaceObjectAtIndex:i+1 withObject:[array objectAtIndex:i]];
                i=i-1;
            }
        [array replaceObjectAtIndex:i+1 withObject:key];
    }
    return  array;
}

Wednesday, September 5, 2012

SQL tips and articles


Recently I found 2 useful SQL functions and would like to share with you.
This function is quite useful for a DBA.
For example:
DECLARE @Object varchar(100)
SELECT @Object = 'Mem.dbo.Member'
SELECT
      PARSENAME (@Object ,3),
      PARSENAME (@Object ,2), 
      PARSENAME (@Object ,1)

You could also apply the same concept to e.g. split IP format
For example: instead of using complex charindex
DECLARE @IP char(15)

SELECT @IP = '192.168.3.11'

SELECT
      PARSENAME (@IP ,4),
      PARSENAME (@IP ,3), 
      PARSENAME (@IP ,2),
      PARSENAME (@IP ,1)   

What it does is returning the first non-null argument. It is equivalent to CASE
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
WHEN (expression2 IS NOT NULL) THEN expression2
...
ELSE expression
END