OC Implementation of heap sorting and OC Implementation of heap sorting

Source: Internet
Author: User

OC Implementation of heap sorting and OC Implementation of heap sorting

/* We recommend that you first check the heap Adjustment Method and understand the heap adjustment. Even if the sorting algorithm has mastered */-(void) viewDidLoad {[super viewDidLoad]; /* test data */NSArray * array = @ [@ 3, @ 2, @ 6, @ 4, @ 1, @ 0, @ 6, @ 7, @ 5]; NSMutableArray * mutable = [NSMutableArray arrayWithArray: array]; mutable = [self createMaxHeap: mutable]; NSInteger num = mutable. count;/* if the number of remaining elements is not 1, continue to adjust and retrieve the elements. Put the retrieved element on the last node. Then reduce the number of heap elements. Therefore, the big top stacks are sorted in ascending order. */While (num> 1) {[mutable exchangeObjectAtIndex: 0 withObjectAtIndex: num-1]; mutable = [self maxHeapAdjust: mutable index: 0 length: num-1]; num --;}} /* adjust the heap recursive adjustment process. The method for adjusting the heap is to pass in the array to be adjusted, the length of the array element (why not use array. count directly? We will dynamically change the length of the unordered heap during sorting, and the length of the array is unchanged, so we do not need array. cout) In fact, every call to adjust the heap method is equivalent to adjusting only three elements: parent node, left and right child nodes. When the left child node is the largest among the three, it is exchanged with the parent node. Then, recursively adjust the three nodes with the parent node (now downgraded to the left child node) as the parent node. Why not adjust the right subnode? This is because the bottom-up adjustments are made during the process of creating a large top stack. At this time, we did not move the right child node, and the right child node and the current parent node (the original left child node) meet the conditions of the big top stack, so no adjustment is required. */-(NSMutableArray *) maxHeapAdjust :( NSMutableArray *) array index :( NSInteger) index length :( NSInteger) length {NSInteger leftChildIndex = index * 2 + 1; // obtain the index of the Left subnode of the node NSInteger rightChildIndex = index * 2 + 2; // obtain the index of the right subnode of the node NSInteger maxValueIndex = index; // temporarily regard this index as the index corresponding to the maximum value // leftChildIndex <length you // array [leftChildIndex]> array [maxValueIndex] determines whether the value of the Left subnode is greater than the current maximum value if (leftChildIndex <length & array [leftChildIndex]> Rray [maxValueIndex]) {// use the index of the Left subnode as the index corresponding to the maximum value maxValueIndex = leftChildIndex ;} // rightChildIndex <length // array [leftChildIndex]> array [maxValueIndex] determines whether the value of the Left subnode is greater than the current maximum value if (rightChildIndex <length & array [rightChildIndex]> array [maxValueIndex ]) {maxValueIndex = rightChildIndex;} // if this node is not the largest node, It is exchanged with the largest node if (maxValueIndex! = Index) {[array exchangeObjectAtIndex: maxValueIndex withObjectAtIndex: index]; // recursive country adjustment. At this time, the value of maxValueIndex is the parent node just now. Array = [self maxHeapAdjust: array index: maxValueIndex length: length];} return array;}-(NSMutableArray *) createMaxHeap :( NSMutableArray *) array {/* start from the last non-leaf node to adjust the heap from the bottom up */for (NSInteger I = (array. count/2-1); I> = 0; -- I) {array = [self maxHeapAdjust: array index: I length: array. count];} return array ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.