Sorting 需要堆排序函数解释
有人能清楚地解释一下这些堆排序函数是如何工作的吗Sorting 需要堆排序函数解释,sorting,heap,Sorting,Heap,有人能清楚地解释一下这些堆排序函数是如何工作的吗 void heapSort(int numbers[], int array_size) { int i, temp; for (i = (array_size / 2)-1; i >= 0; i--) siftDown(numbers, i, array_size); for (i = array_size-1; i >= 1; i--) { temp = numbers[0]; numb
void heapSort(int numbers[], int array_size)
{
int i, temp;
for (i = (array_size / 2)-1; i >= 0; i--)
siftDown(numbers, i, array_size);
for (i = array_size-1; i >= 1; i--)
{
temp = numbers[0];
numbers[0] = numbers[i];
numbers[i] = temp;
siftDown(numbers, 0, i-1);
}
}
void siftDown(int numbers[], int root, int bottom)
{
int done, maxChild, temp;
done = 0;
while ((root*2 <= bottom) && (!done))
{
if (root*2 == bottom)
maxChild = root * 2;
else if (numbers[root * 2] > numbers[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;
if (numbers[root] < numbers[maxChild])
{
temp = numbers[root];
numbers[root] = numbers[maxChild];
numbers[maxChild] = temp;
root = maxChild;
}
else
done = 1;
}
}
void heapSort(整数[],整数数组大小)
{
int i,温度;
对于(i=(数组大小/2)-1;i>=0;i--)
siftDown(数字、i、数组大小);
对于(i=array_size-1;i>=1;i--)
{
温度=数字[0];
数字[0]=数字[i];
数字[i]=温度;
siftDown(数字,0,i-1);
}
}
void siftDown(整数[],整数根,整数底)
{
int done,maxChild,temp;
完成=0;
while((根*2个数字[根*2+1])
maxChild=根*2;
其他的
maxChild=根*2+1;
if(数字[根]<数字[maxChild])
{
温度=数字[根];
数字[root]=数字[maxChild];
数字[maxChild]=温度;
root=maxChild;
}
其他的
完成=1;
}
}
用堆排序的图表进行了充分的解释。这有助于将其视为一场比赛:首先插入所有球员,使排名靠前的球员成为赢家。然后提取赢家,将输家提升为新赢家,并进行调整,以便再次获得一场适当的比赛,新赢家是比赛中的佼佼者缅因球员
然后进行迭代。公共类heapsort
public class heapsort
{
public static void buildheap(int[] a, int nextLimit)
{
// for parent 3 child is 3*2+1=7 and 3*2+2=8 hence parent if odd n+1/2-1 i.e (7+1)/2-1=3 for odd n/2-1 8/2-1=3
int child = nextLimit % 2 == 1 ? nextLimit + 1 : nextLimit;
for (int parent = child / 2 - 1; parent >= 0; parent--) {
heapfy(a, parent, nextLimit);
}
}
public static void heapfy(int[] a, int parentIndex, int limit)
{
int maxChildIndex;
//if parent have only one child (java array index start from 0 hence left one 2i+1 and right one 2i+2)
if ((2 * parentIndex + 2) > limit) {
maxChildIndex = 2 * parentIndex + 1;
} else {
//find max value index from two child
maxChildIndex = a[2 * parentIndex + 1] > a[2 * parentIndex + 2] ? 2 * parentIndex + 1 : 2 * parentIndex + 2;
}
//swap if parent less than max child bring max value to parent
if (a[maxChildIndex] > a[parentIndex]) {
int maxValue = a[maxChildIndex];
a[maxChildIndex] = a[parentIndex];
a[parentIndex] = maxValue;
}
}
public static void main(String[] args)
{
int[] a = {2, 5, 4, 6, 77, 3, 1, 8};
for (int nextArrayLength = a.length - 1; nextArrayLength >= 0; nextArrayLength--) {
buildheap(a, nextArrayLength);
//push to first to last
int highest = a[0];
a[0] = a[nextArrayLength];
a[nextArrayLength] = highest;
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
{
公共静态void构建堆(int[]a,int nextLimit)
{
//对于父3,子3*2+1=7,3*2+2=8,因此如果奇数n+1/2-1,即(7+1)/2-1=3,则奇数n/2-1 8/2-1=3,则为父
int child=nextLimit%2==1?nextLimit+1:nextLimit;
对于(int parent=child/2-1;parent>=0;parent--){
heapfy(a,父母,下一个限制);
}
}
公共静态无效堆(int[]a,int parentIndex,int limit)
{
int-maxChildIndex;
//如果父级只有一个子级(java数组索引从0开始,因此左一个2i+1,右一个2i+2)
如果((2*parentIndex+2)>限制){
maxChildIndex=2*parentIndex+1;
}否则{
//从两个子对象中查找最大值索引
maxChildIndex=a[2*parentIndex+1]>a[2*parentIndex+2]?2*parentIndex+1:2*parentIndex+2;
}
//如果父级小于最大子级,则交换将最大值带到父级
如果(a[maxChildIndex]>a[parentIndex]){
int maxValue=a[maxChildIndex];
a[maxChildIndex]=a[parentIndex];
a[parentIndex]=最大值;
}
}
公共静态void main(字符串[]args)
{
int[]a={2,5,4,6,77,3,1,8};
对于(int-nextArrayLength=a.length-1;nextArrayLength>=0;nextArrayLength--){
buildheap(a,nextArrayLength);
//推前置后
int最高=a[0];
a[0]=a[nextArrayLength];
a[nextArrayLength]=最高;
}
for(int i=0;i
考虑到以下问题,用户名的选择很有趣:)我也觉得OP选择论坛名称“Codeguru”有点幽默。查看这些简短清晰的视频:算法:;动画和伪代码: