Skip to content

Instantly share code, notes, and snippets.

@marioeguiluz
Last active August 29, 2015 14:20
Show Gist options
  • Save marioeguiluz/965dec147cbceb620c24 to your computer and use it in GitHub Desktop.
Save marioeguiluz/965dec147cbceb620c24 to your computer and use it in GitHub Desktop.
Insertion Sort Objective-C
//Insertion Sort of an array of integers
//**************************************
NSMutableArray* insertionSort(NSMutableArray* unsortedArray){
if(!unsortedArray) return nil;
if(unsortedArray.count<2) return unsortedArray;
for (int j=1; j<unsortedArray.count; j++) {
int i = j;
while(i>0 && [[unsortedArray objectAtIndex:(i-1)] intValue] > [[unsortedArray objectAtIndex:i] intValue])
{
[unsortedArray exchangeObjectAtIndex:i withObjectAtIndex:(i-1)];
i--;
}
}
return unsortedArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment