The method of sorting by heaps arranges the numbers in the array from small to large.
Compiler: GCC, compile environment: 32-bit system can compile normally; If it is a 64-bit system, use the-m32 parameter, or modify the assembly code section in the macro definition.
#include <stdio.h> #include <stdlib.h> #include <strings.h> #define PUSH (x) __asm__ volatile ("Push% %eax "::" A "(x)) #define POP () ({register int _res; __asm__ volatile ("Pop%%eax": "=a" (_res):); _res;}) typedef struct NODE{INT data; int seq;struct node *left;struct node *right;} Node;node *head=null;void Exchange (node *a,node *b) {int tmp; tmp=a->data; a->data=b->data; B->data=tmp;} Node *find_node (int i,node *head) {node *p=head; int digit=0; int ii=i; while (ii!=1) {push (ii&1); digit++; ii>>=1; } while (digit) {if (pop () ==1) {p=p->right; }else{p=p->left; } digit--; } return p;} int compare_up (int i) {int tmp; if (i==1) return 1; else{if (Find_node (i/2,head)->data > Find_node (i,head)->data) {Tmp=find_node (i/2,head)->da Ta Find_node (i/2,head)->data = Find_node (i,head)->data; Find_node (I,head)->data=tmp; return I/2; }else{return i; }}}void adjust_up (int i) {int tmp; while (1) {tmp=compare_up (i); if (tmp==i) break; else i=tmp; }}node *compare_down (node *p) {node *tmp; if (!p) {return p; } if (P->left!=null && p->right!=null) {if (P->left->data > P->right->data) { tmp=p->right; }else{tmp=p->left; } if (P->data > Tmp->data) {Exchange (P,TMP); return (TMP); }}else if (P->left==null && p->right!=null) {if (P->right->data < P->data) { Exchange (P->RIGHT,P); return (p->right); }}else if (P->left!=null && p->right==null) {if (P->left->data < P->data) {Exchange (P->LEFT,P); return (P->left); }} return p;} void Adjust_down (node *head) {node *tmp; Node *p=head; while (1) {Tmp=compare_down (P); if (tmp==p) break; else p=tmp; } printf ("%d", Head->data);} void Mount_node (int i,int data) {node *tmp; int digit=0; int ii=i; Node *p; if (i==1) {head= (node *) malloc (sizeof (node)); if (!head) {printf ("1:malloc error!\n"); Return } bzero (head,sizeof (node)); head->data=data; head->seq=i; }else{tmp= (node *) malloc (sizeof (node)); if (!tmp) {printf ("2:malloc error!\n"); Return } bzero (tmp,sizeof (node)); tmp->data=data; tmp->seq=i; while (ii!=1) {push (ii&1); digit++; ii>>=1; } digit--; P=head; while (digit) { if (pop () ==1) {p=p->right; }else{p=p->left; } digit--; } if (pop () ==1) {p->right=tmp; }else{p->left=tmp; }}}int Main (void) {/* to sort target */int data[]={23,45,1,-3,67,100,90,-2,1,1}; /* The head node sequence number is 1, which is summed backwards and forwards */int i=1; Node *tmp; /*for Loop builds the heap */for (; i<= (sizeof (data)/sizeof (int)), i++) {/* Generates a new node, hangs on a full binary tree */Mount_node (i,data[i-1]); /* Adjust the heap once per insertion of a node, at which point the adjustment is adjusted from the current node to the top/adjust_up (i); } i--; /* Start output after building the heap */while (i) {/* because it is starting from the beginning of the node output, each output one will be adjusted once the heap, the adjustment is from the head node downward adjustment */Adjust_down (head); /* Swap the end node in the heap with the head node, and the next while loop adjusts the heap for this change/Tmp=find_node (I,head); Exchange (Tmp,head); /* Release the end node */free (TMP); /* Clears the parent node pointer for the end node */if (i!=1) {if (i&1) {Find_node (i/2,head)->right=null; }else{Find_node (I/2,head)->left=null; }} i--; } printf ("\ n");}
Operation Result:
A complex heap sequencing program