Home > Back-end >  A little problem about heap sort, for help bosses, I very vegetables, don't spray.
A little problem about heap sort, for help bosses, I very vegetables, don't spray.

Time:12-01

Is a time when I sort of found problems always have the position of the two Numbers, as shown in figure
This is the implementation of the code
 # define _CRT_SECURE_NO_WARNINGS 
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Time. H>

//adjustment (reconstruction) root pile, if you want to from large to little is needed to establish the root pile
Void BigHeap_Adjust (int input [], int m, int s)//input [s] for the root node, m is the total nodes, this is a big adjustment of root pile
{
Int temp=input [s].//save node data
For (int j=2 * s; J & lt;=m; J *=2)//for the first s node, screening until the end of the leaf node
{
If (((j + 1) & lt; M) & amp; & (input [j] If (temp & gt; [j]=input) break;//if the current node is greater than the maximum of child nodes, direct skip
Input [s]=input [j];//if the current node values than the child nodes of the largest value is small, will be the largest value moves to the node
S=j;
}
Input [s]=temp;//in the end put the value of the temp in the right place
}
Void SmallHeap_Adjust (int input [], int m, int s)
{
Int temp=input [s].
For (int j=2 * s; J & lt;=m; J *=2)
{
If (((j + 1) & lt; M) & amp; & (input [j] & gt; Input j++ [j + 1)));//find the value of the youngest child node j
If (temp & lt; [j]=input) break;//if the current node to a smaller value than the youngest child nodes, direct skip
Input [s]=input [j];//if the value of the current node is greater than the lowest value in a child node, and then remove the greatest value to the node
S=j;
}
Input [s]=temp;//in the end put the value of the temp in the right place
}
Void BigHeap_Sort (int * input, int length)
{
Int I, temp, n=length;
//new heap
For (int I=length/2; I & gt;=1; I -)//since the first length/2 records to filter built pile
BigHeap_Adjust (input, length, I);

For (I=n; I & gt;=1; I -)
{
Temp=input [1].//record pile top and pile the last record of the swap, which remove the pile top
Input [1]=input [I];
Input [I]=temp;
if(i!=1) BigHeap_Adjust (input, I - 1, 1);//adjustment, make the rest of the pile of
}
}
Void SmallHeap_Sort (int * input, int length)
{
Int I, temp, n=length;
//new heap
For (int I=length/2; I & gt;=1; I -)//since the first length/2 records to filter built pile
SmallHeap_Adjust (input, length, I);

For (I=n; I & gt;=1; I -)
{
Temp=input [1].//record pile top and pile the last record of the swap, which remove the pile top
Input [1]=input [I];
Input [I]=temp;
If (I.=1) SmallHeap_Adjust (input, I - 1, 1);//adjustment, make the rest of the pile of
}
}
Int main ()
{
Int a, b, * p;
char i;
Srand ((unsigned) time (NULL));
Printf (" \ n (Heap Sort) please enter a number to Sort the number of \ n ");
The scanf (" % d ", & amp; A);
P=(int *) malloc ((a + 1) * sizeof (int));
if (p !=NULL) printf (" the Memory allocation successful! \n");
For (I=1; I & lt;=a; I++)
{
B=rand () % (1001);
Printf (" please enter the first number: % d % d \ n ", I, b);
P [I]=b;
}
Printf (" input the results as follows: \ n ");
For (I=1; I & lt; A + 1; I++)
Printf (" % d \ t ", p [I]);

BigHeap_Sort (p, a);
Printf (" \ n the smallest results are as follows: \ n ");
For (I=1; I & lt; A + 1; I++)
Printf (" % d \ t ", p [I]);

SmallHeap_Sort (p, a);
Printf (" \ n the ranking results from large to small is as follows: \ n ");
For (I=1; I & lt; A + 1; I++)
Printf (" % d \ t ", p [I]);
printf("\n");

system("pause");
return 0;
}

CodePudding user response:

//adjustment (reconstruction) large root pile
Void BigHeap_Adjust function here is wrong, likewise SmallHeap_Adjust is wrong,
  • Related