Created
May 14, 2015 00:01
-
-
Save marioeguiluz/c1ca0d68d04f27777d9e to your computer and use it in GitHub Desktop.
HeapSort 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
////////////////HEAP SORT//////////////// | |
int leftLeafIndex(int rootIndex){ | |
int heapIndex = rootIndex+1; | |
return heapIndex*2-1; | |
} | |
int rightLeafIndex(int rootIndex){ | |
int heapIndex = rootIndex+1; | |
return heapIndex*2+1-1; | |
} | |
int heapLastIndex (NSMutableArray* A){ | |
return (int)A.count-1; | |
} | |
void maxHeapify(NSMutableArray* A, int indexRoot){ | |
if(leftLeafIndex(indexRoot)>heapLastIndex(A)){ | |
return; | |
} | |
int rootValue =[A[indexRoot] intValue]; | |
int largestIndex = indexRoot; | |
int largestValue = rootValue; | |
int leftLeafValue = [A[leftLeafIndex(indexRoot)] intValue]; | |
if(leftLeafValue>rootValue) { | |
largestIndex = leftLeafIndex(indexRoot); | |
largestValue = leftLeafValue; | |
} | |
if(rightLeafIndex(indexRoot)<=heapLastIndex(A)){ | |
int rightLeafValue = [A[rightLeafIndex(indexRoot)] intValue]; | |
if(rightLeafValue>largestValue) { | |
largestIndex = rightLeafIndex(indexRoot); | |
largestValue = rightLeafValue; | |
} | |
} | |
if(largestIndex != indexRoot){ | |
[A exchangeObjectAtIndex:indexRoot withObjectAtIndex:largestIndex]; | |
maxHeapify(A, largestIndex); | |
} | |
} | |
void buildMaxHeap(NSMutableArray* A){ | |
if(A.count<2) return; | |
int lastParentIndex = (int)A.count/2; | |
for (int parentIndex = lastParentIndex; parentIndex >= 0; parentIndex--) { | |
maxHeapify(A, parentIndex); | |
} | |
} | |
NSMutableArray* heapSort(NSMutableArray* unsortedA){ | |
if(unsortedA.count<2) return unsortedA; | |
buildMaxHeap(unsortedA); | |
NSMutableArray* sortedA = [NSMutableArray new]; | |
for (int i = (int)unsortedA.count-1; i>0; i--) { | |
[sortedA insertObject:unsortedA[0] atIndex:0]; | |
[unsortedA exchangeObjectAtIndex:0 withObjectAtIndex:unsortedA.count-1]; | |
[unsortedA removeLastObject]; | |
maxHeapify(unsortedA, 0); | |
} | |
[sortedA insertObject:unsortedA[0] atIndex:0]; | |
return sortedA; | |
} | |
//Example of use to sort an array: | |
/* | |
NSMutableArray * unsortedArray = [@[@4,@1,@3,@2,@16,@9,@10,@14,@8,@7] mutableCopy]; | |
NSMutableArray * sortedArray = heapSort(unsortedArray); | |
NSLog(@"%@",[sortedArray description]); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love this implementation of heap sort in objective-c. I have one question regarding left and right leaf indexes, how does the math on this work. Based on my understanding this actually would be grabbing a child node and a parent node, and yet it totally works out. Why is this? And is it possible to run an implementation that uses a leaf node structure more closely matching a traditional array to tree approach ie parentIndex *2 for left and parentIndex *2 +1 for right? Thanks for any insight into this and thanks for posting this awesome algorithm