Beispiel #1
0
void HeapSort(int data[], int N)
{
	int j;
	int tmp;
	
	//Build heap
	//建堆的过程是从最后一个非叶子结点开始向上调整堆的过程
	for (j=(N/2-1); j>=0; j--)		//the last non-leaf node is N/2-1
	{
		HeapAdjust(data, N, j);
	}

	//Now we have a big heap
	//exchange the maximum node with the last node and then adjust heap
	for (j=N-1; j>0; j--)
	{
		//exchange
		tmp = data[j];
		data[j] = data[0];
		data[0] = tmp;

		//adjust heap
		HeapAdjust(data, j, 0);
	}
}
Beispiel #2
0
void HeapSort(struct ArrayData _arrayData[], int n) {
	for (int i = n / 2 - 1; i >= 0; i--) {
		HeapAdjust(_arrayData, i, n - 1);
	}
	
	for (int i = n - 1; i > 0; i--) {
		swap(&_arrayData[0], &_arrayData[i]);
		HeapAdjust(_arrayData, 0, i - 1);
	}
}
Beispiel #3
0
void HeapSort(int heap[], int hpLength) {
    int i;

    for (int i = hpLength / 2; i > 0; --i)
        HeapAdjust(heap, i, hpLength);
    for (int i = hpLength; i > 1; --i) {
        swap(&heap[1], &heap[i]);
        HeapAdjust(heap, 1, i - 1);
    }
}
Beispiel #4
0
void HeapSort(TList *l)
{
	int i;
	for(i=l->length/2; i>0; i--)
		HeapAdjust(l, i, l->length);
	
	for(i=l->length; i>1; i--)
	{
		swap(l, 1, i);
		HeapAdjust(l, 1, i-1);
	}	
}
void HeapSort(int a[],int N)
{
    int i,t;
    for(i=N/2;i>0;i--)
        HeapAdjust(a,i,N);
    for(i=N;i>=1;i--)
    {
        t=a[i];
        a[i]=a[1];
        a[1]=t;
        HeapAdjust(a,1,i-1);
    }
}
Beispiel #6
0
void HeapSort(SqList &L)
{//对顺序表L做堆排序。
    int i,j,t;
    for(i=L.length/2;i>0;--i)   //把L.elemword[1..L.length]建成大顶堆
        HeapAdjust(L,i,L.length);
    for(i=L.length;i>1;--i)
    {
        t=L.elemword[1];             //将堆顶记录和当前未经排序子序列L.elemword[1..i]
        L.elemword[1]=L.elemword[i]; //中的最后一个记录相互交换
        L.elemword[i]=t;
        HeapAdjust(L,1,i-1);         //将L.r[1..i-1]重新调整为大顶堆
    }
}
Beispiel #7
0
void HeapSort(int a[],int n)
{
    for(int i = n/2 - 1;i >= 0;i--)
    {
	HeapAdjust(a,i,n);
    }
    for(int i = n - 1;i >= 1;i--)
    {
	int tmp = a[0];
	a[0] = a[i];
	a[i] = tmp;
	HeapAdjust(a,0,i);
    }
}
Beispiel #8
0
// -----------------------------------------------------------------------------
void Algorithms::HeapSort(std::vector<int> & v)
{
    int v_length = static_cast<int>(v.size());

    // Heapify the initial v.
    for (int i = v_length/2; i >= 0; --i) {
        HeapAdjust(v, i, v_length);
    }

    // Sort.
    for (int i = v_length - 1; i > 0; --i) {
        std::swap(v[0], v[i]);
        HeapAdjust(v, 0, i);
    }
}
/**
 * Function : HeapSort
 * Description : heap sort
 * Input : 
 * Output : nothing
 * return : nothing
 */
void HeapSort(Queue_Pat *Prio_Que) {
	//对队列Prio_Que进行堆排序
	int i;
	Pat exchange_p;
	for(i = (Prio_Que -> length) / 2 ; i > 0 ; -- i) {
		//把(Prio_Que->base)[1..Prio_Que->length]建成大顶堆
		HeapAdjust(Prio_Que , i , Prio_Que -> length);
	}
	for(i = Prio_Que -> length ; i > 1 ; -- i) {
		//将堆顶记录和当前未经排序的子序列中最后一个交换
		exchange_p = (Prio_Que->base)[1];
		(Prio_Que->base)[1] = (Prio_Que->base)[i];
		(Prio_Que->base)[i] = exchange_p;
		HeapAdjust(Prio_Que , 1 , i - 1);
	}
}//HeapSort
Beispiel #10
0
//建立堆
void BuildHeap(int *a,int n){

    for (int i = n - 1; i >= 0; i--) {
        HeapAdjust(a, i, n);
    }

}
void BuildHeap(int *a, int size)    //建立堆 
{
    int i;
    for(i = size / 2; i >= 0; i--)    //非叶节点最大序号值为size/2 
    {
        HeapAdjust(a, i, size);    
    }    
} 
Beispiel #12
0
void
HeapAdjust1(int request, int unused, int withhold, double rate, int phases,
	Heap_t* from1, Heap_t* to)
{
	Heap_t *froms[2];
	froms[0] = from1;
	froms[1] = NULL;
	HeapAdjust(request, unused, withhold, rate, phases, froms, to);
}
void HeapSort(int L[], int length){

	// 构建堆是一个循环过程, 即从下往上,从左往右,对所有的分支结点进行堆调整。
	for (int i = length / 2; i > 0; i--){
	
		HeapAdjust(L, i, length);
	}
	std::cout << "Finish Constructing Heap..." << std::endl;
	HeapSort_print(L, length);

	for (int j = 1; j < length; j++){
	
		swap(L, 1, length - j + 1);
		HeapAdjust(L, 1, length - j);
		std::cout << "The " << j << " runs:" << std::endl;
		HeapSort_print(L, length);
	}
}
Beispiel #14
0
void
HeapAdjust2(int request, int unused, int withhold, double reserve, int phases,
	Heap_t* from1, Heap_t* from2, Heap_t* to)
{
	Heap_t *froms[3];
	froms[0] = from1;
	froms[1] = from2;
	froms[2] = NULL;
	HeapAdjust(request, unused, withhold, reserve, phases, froms, to);
}
Beispiel #15
0
//堆排序
void HeapSort(int *a,int n){

    BuildHeap(a, n);
    for (int i = n - 1; i >= 0; i--) {
        //交换堆顶和最后一个元素,即每次将剩余元素中的最大者放到后面;
        swap(&a[0], &a[i+1]);
        //重新调整堆为大顶堆;
        HeapAdjust(a, 0, i );
    }
}
void Heapsort(SqList *l)
{
    int i,compare,change;
    for(i=l->len/2;i>0;i--)
        HeapAdjust(l,i,l->len);
    for(i=l->len;i>1;i--)
    {
        l->r[0]=l->r[1];
        l->r[1]=l->r[i];
        l->r[i]=l->r[0];
        change++;
        HeapAdjust(l,1,i-1);
    }
    printf("\n-----小顶堆排序后顺序-----\n");
    for(i=1;i<=l->len;i++)
        printf("%d  ",l->r[i].key);
    printf("\n");
    printf("小顶堆排序比较次数为:%d\n",compare);
    printf("小顶堆排序移动次数为:%d\n",change*3);
}
void HeapSort(int *a, int size)    //堆排序 
{
    int i;
    BuildHeap(a, size);
    for(i = size - 1; i > 0; i--)
    {
        swap(a[0], a[i]);           //交换堆顶和最后一个元素,即每次将剩余元素中的最大者放到最后面 
		//BuildHeap(a, i-1);        //将余下元素重新建立为大顶堆 
		HeapAdjust(a, 0, i);  		//重新调整堆顶节点成为大顶堆
    }
} 
Beispiel #18
0
void HeapSort(int a[], int n)
{
    int i, temp;
    
    for(i = n/2; i >= 1; --i)
    {
        temp = a[1];
        a[1] = a[i+1];
        a[i=1] = temp;
        HeapAdjust(a, 1, i);
    }
}
Beispiel #19
0
// 堆排序算法
void HeapSort(count* array,int length)
{ 
    int i;
	count tmp;
    // 调整序列的前半部分元素,调整完之后第一个元素是序列的最大的元素
    //length/2-1是第一个非叶节点,此处"/"为整除
    for (i = length -1/ 2 ; i >= 0; --i)
        HeapAdjust(array, i, length);
    // 从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素
    for (i = length - 1; i > 0; --i)
    {
        // 把第一个元素和当前的最后一个元素交换,
        // 保证当前的最后一个位置的元素都是在现在的这个序列之中最大的
      ///  Swap(&array[0], &array[i]);
          tmp = array[i];
          array[i] = array[0];
          array[0] = tmp;
        // 不断缩小调整heap的范围,每一次调整完毕保证第一个元素是当前序列的最大值
        HeapAdjust(array, 0, i);
    }
}
Beispiel #20
0
void HeapSort(int* pData, int nLength)
{
	int nTemp = 0;
	int i = nLength / 2 - 1;

	// 该for循环结果使[0, nLength-1]中的数据成为了堆。
	for (; i >= 0; --i)
	{
		// 为pData[i]值找位置。
		HeapAdjust(pData, i, nLength);
	}

	// 每趟循环把[0, i]中的最大值pData[0]和末尾的pData[i]值进行交换。
	for (i = nLength - 1; i > 0; --i)
	{
		// 交换pData[0]和pData[i]的值
		nTemp = pData[i];
		pData[i] = pData[0];
		pData[0] = nTemp;

		// 由于pData[0]值改变了,所以需要调整,使[0, i-1]中的数据成为堆
		HeapAdjust(pData, 0, i);
	}
}
Beispiel #21
0
//调整堆
void HeapAdjust(int *a,int i,int n){

    int lchild = 2 * i;//左孩子节点;
    int rchild = 2 * i + 1;//右孩子节点;
    int max = i;

    if (i <= n) {
        if (lchild <= n && a[lchild] > a[max]) {
            max = lchild;
        }

        if (rchild <= n && a[rchild] > a[max]) {
            max = rchild;
        }

        if (i != max) {
            swap(&a[i], &a[max]);
            //避免调整之后以max为父节点的子树不是堆;
            HeapAdjust(a, max, n);
        }
    }
}
/**
 * @brief 把二叉树中以i为根的子树变成最大堆。
 * @brief 注意: 使用的前提条件是i节点的左右子树(如果存在的话)都是最大堆
 * @param a
 * @param i
 * @param size
 */
void HeapAdjust(int *a, int i, int size)  //调整堆 
{
    int lchild = 2 * i;       	//i的左孩子节点序号 
    int rchild = 2 * i + 1;   	//i的右孩子节点序号 
    int max = i;         		//临时变量 
    if(i <= size / 2)        	//如果i是叶节点就不用进行调整 
    {
        if(lchild < size && a[lchild] > a[max])
        {
            max = lchild;
        }    
        if(rchild < size && a[rchild] > a[max])
        {
            max = rchild;
        }
        if(max != i)
        {
            swap(a[i], a[max]);
            HeapAdjust(a, max, size);    //避免调整之后以max为父节点的子树不是堆 
        }
    }        
}
Beispiel #23
0
// -----------------------------------------------------------------------------
void Algorithms::HeapAdjust(std::vector<int> & v, int root_index, int end)
{
    int left_child_index = 2 * root_index + 1;
    int right_child_index = left_child_index + 1;

    if (left_child_index >= end) {
        // No left child. Return.
        return;
    }

    // Get the index of the larger child.
    int max_child_index = left_child_index; // initial value
    if (right_child_index < end && v[left_child_index] < v[right_child_index]) {
        max_child_index = right_child_index;
    }

    // If the current root < the larger child, then swap the values and
    // adjust the subtree that starts from the larger child.
    if (v[root_index] < v[max_child_index]) {
        std::swap(v[root_index], v[max_child_index]);
        HeapAdjust(v, max_child_index, end);
    }
}