示例#1
0
void AllSort::HeapSort() {
  for (int i = array_size_/2 - 1; i >= 0; --i) {
    Sift(i, array_size_);
  }
  for (int j = array_size_ - 1; j >= 0; --j) {
    Swap(0, j);
    Sift(0, j - 1);
  }
}
示例#2
0
void HeapSort(std::vector<T> &vec)
{
	if (vec.empty()) return;
	int n = vec.size();
	int i;
	for (i = n / 2; i >= 1; --i)	//循环建立初始堆(从最后一个非叶子结点开始)
		Sift(vec, i, n);
	for (i = n; i >= 2; i--)
	{
		swap(vec[0], vec[i - 1]);
		Sift(vec, 1, i - 1);
	}
}
示例#3
0
void HeapSort2(int* arr, int n)
{
	int i;
	for(i = n / 2; i >= 1; i--)
	{
		Sift(arr, i, n);
	}
	for(i = 1; i < n; i++)
	{
		swap(arr[1], arr[n-i+1]);
		Sift(arr, 1, n-i);	
	}
}
示例#4
0
void BuildHeap2(int* arr, int n)
{
	for(int i = n / 2; i >= 1; i--)
	{
		Sift(arr, i, n);
	}
}
示例#5
0
void HeapSort(T* pArr, size_t Size)
{
	if (!pArr || Size == 0)
		return;

	size_t i = Size / 2;

	while(i--)
	{
		Sift(pArr, i, Size);
	}


	i = Size;

	while(--i)
	{
		std::swap(pArr[0], pArr[i]);
		Sift(pArr, 0, i);
	}
}
示例#6
0
// Sift the i-th item in the heap.
void AllSort::Sift(int i, int length) {
  // CHECK_LT(i, length);
  int l_child = i * 2 + 1;
  int r_child = l_child + 1;
  int position = i;
  // Create a max-heap.
  if ((l_child < length) && (array_[i] > array_[l_child])) {
    position = l_child;
  }
  if ((r_child < length) && (array_[position] > array_[r_child])) {
    position = r_child;
  }
  if (position != i) {
    Swap(i, position);
    Sift(position, length);
  }
}
示例#7
0
文件: model.c 项目: swails/ambermini
/*
 *	zModelOrderAtoms
 *
 *	Author:	Christian Schafmeister (1991)
 *
 *	Order the atoms bonded to aX (other than aY) into
 *	the array aaX[].  Sift the atoms in aaX by whether or not
 *	they have their coordinates defined.  Those with coordinates
 *	defined are first in the array and those without are last.
 *
 *	Put the number of atoms in (iX)
 *	and the index of the first atom with undefined coordinates in
 *	*iPFirstXUnknown.  Within each group of atoms place the
 *	atom with the largest 'weight' at the start of the group.
 *	This forces the 'heaviest' atoms to be trans to each other.
 */
static void
zModelOrderAtoms( MODELATOMt *maPX, MODELATOMt *maPY, MODELATOMt maaXBonds[],
                  int iX, int *iPFirstXUnknown )
{
    int		i, iHighest, iPos, iWeight;
    MODELATOMt	maTemp;

    /* Sift the atoms by whether or not they have defined */
    /* coordinates */


    Sift( maaXBonds, sizeof(maaXBonds[0]), iX,
          zbModelTrueIfPosKnown, iPFirstXUnknown );

    /* Find the 'heaviest' atom in the second half of the */
    /* list and put it at the front */

    iHighest = 0;
    for ( i=0; i<*iPFirstXUnknown; i++ ) {
        iWeight = ziModelAtomWeight(maaXBonds[i].aAtom);
        if ( iHighest < iWeight ) {
            iHighest = iWeight;
            iPos = i;
        }
    }
    if (iHighest!=0) SWAP( maaXBonds[0], maaXBonds[iPos], maTemp );

    /* Find the 'heaviest' atom in the second half of the */
    /* list and put it at the front */

    iHighest = 0;
    for ( i=*iPFirstXUnknown; i<iX; i++ ) {
        iWeight = ziModelAtomWeight(maaXBonds[i].aAtom);
        if ( iHighest < iWeight ) {
            iHighest = iWeight;
            iPos = i;
        }
    }
    if ( iHighest != 0 )
        SWAP( maaXBonds[*iPFirstXUnknown], maaXBonds[iPos], maTemp );
}