Insertion sort

Insertion sort is a simple sorting algorithm:

+(NSInteger)InsertionSort:(NSMutableArray*)array
{
    for (NSInteger index = 1; index < [array count]; index++) {
        NSInteger currentValue = [array[index] integerValue];
        NSInteger position = index;
        while (position > 0 && ([array[position -1] integerValue] > currentValue 
             {
             array[position] = array[position-1];
             steps++;
             position = position-1;
             }
       array[position] = @(currentValue);
       }
       return steps;
}

					

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s