One-step write algorithm (heap sorting)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Heap sorting is another common recursive sorting method. Because heap sorting has excellent sorting performance, it is often used in software design. Heap sorting has its own special nature, which is basically the same as that of the binary balancing tree. For example, every data in a large pile must meet the following requirements:

(1) Each array [N] is not less than array [2 * n]

(2) Each array [N] is not smaller than array [2 * n + 1]

Building such a heap is just the foundation. Next we need to remove a data from the top of the heap and constantly adjust the heap until the array turns into an ordered array. Therefore, the detailed heap sorting algorithm should be as follows:

1) build a large heap so that every data in the heap meets the nature mentioned above

2) swap the first data in the heap with the last data in the heap, and re-adjust the heap until the heap is re-balanced.

3) Repeat 2) until the entire array is ordered.

The process described above is very simple. What is the practice?

A) Judge the input parameter

void heap_sort(int array[], int length){if(NULL == array || 0 == length)return ;/* to make sure data starts at number 1 */_heap_sort(array-1, length);}


B) Build and adjust the stack

void _heap_sort(int array[], int length){int index = 0;int median = 0;construct_big_heap(array, length);for(index = length; index > 1; index --){median = array[1];array[1] = array[index];array[index] = median;reconstruct_heap(array, 1, index-1);}}


C) Detailed operations for building a large pile

void set_sorted_value(int array[], int length){int index = length;int median = 0;if(length == 1) return;while(index > 1){if(array[index >> 1] >= array[index])break;median = array[index];array[index] = array[index >> 1];array[index >> 1] = median;index >>= 1;}}void construct_big_heap(int array[], int length){int index = 0 ;for(index = 1; index <= length; index ++){set_sorted_value(array, index);}}

D) iterative adjustment of large stacks

void reconstruct_heap(int array[], int index, int length){int swap = 0;if(length < index << 1)return;if(length == index << 1){adjust_leaf_position(array, index);return;}if(-1 != (swap = adjust_normal_position(array, index))){reconstruct_heap(array, swap, length);}}

E) process a single branch node and a full branch node respectively.

int adjust_normal_position(int array[], int index){int left = index << 1 ;int right = left + 1;int median = 0;int swap = 0;if(array[index] >= array[left]){if(array[index] >= array[right]){return -1;}else{swap = right;}}else{if(array[index] >= array[right]){swap = left;}else{swap = array[left] > array[right] ? left : right;}}if(swap == left) {median = array[index];array[index] = array[left];array[left] = median;}else{median = array[index];array[index] = array[right];array[right] = median;}return swap;}STATUS adjust_leaf_position(int array[], int index){int median = 0;if(array[index] > array[index << 1])return TRUE;median = array[index];array[index] = array[index << 1];array[index << 1] = median;return FALSE;}


F) after the heap sorting algorithm is introduced, create a test case for verification.

static void test1(){int array[] = {1};heap_sort(array, sizeof(array)/sizeof(int));}static void test2(){int array[] = {2, 1};heap_sort(array, sizeof(array)/sizeof(int));assert(1 == array[0]);assert(2 == array[1]);}static void test3(){int array[] = {3, 2, 1};heap_sort(array, sizeof(array)/sizeof(int));assert(1 == array[0]);assert(2 == array[1]);assert(3 == array[2]);}static void test4(){int array[] = {2, 3, 1};heap_sort(array, sizeof(array)/sizeof(int));assert(1 == array[0]);assert(2 == array[1]);assert(3 == array[2]);}static void test5(){int array[] = {5,3, 4, 1};heap_sort(array, sizeof(array)/sizeof(int));assert(1 == array[0]);assert(3 == array[1]);assert(4 == array[2]);assert(5 == array[3]);}static void test6(){int array[] = {2, 3,6, 8, 7};heap_sort(array, sizeof(array)/sizeof(int));assert(2 == array[0]);assert(3 == array[1]);assert(6 == array[2]);assert(7 == array[3]);assert(8 == array[4]);}static void test7(){int array[] = {3,4,2,7,1,9,8,6,5};heap_sort(array, sizeof(array)/sizeof(int));assert(1 == array[0]);assert(2 == array[1]);assert(3 == array[2]);assert(4 == array[3]);assert(5 == array[4]);assert(6 == array[5]);assert(7 == array[6]);assert(8 == array[7]);assert(9 == array[8]);}

[Notice: The following blog introduces some common data structures]

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.