Last active
August 29, 2015 14:20
-
-
Save marioeguiluz/965dec147cbceb620c24 to your computer and use it in GitHub Desktop.
Insertion Sort Objective-C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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