Beispiel #1
0
void heapSort0(int* a, int size)
{
    for(int k=size/2+1; k >= 0; k--)
        fixDown(a, size, k);
    while(size > 1)
    {
        swap(a, a+size-1);
        fixDown(a, --size, 0);
    }
}
Beispiel #2
0
//More portable;
void headSort1(int *a, int l, int r)
{
    int size = r-l+1;
    for(int k=size/2; k >= 0; k--)
        fixDown(a+l, size, k);
    while(size > 1)
    {
        swap(a+l, a+l+size-1);
        fixDown(a+l, --size, 0);
    }
}
Beispiel #3
0
void heapSort(int *arr, int l)
{
    int r;
    for (r=(l+1)/3; r>0; r--)
    {
        fixDown(arr, r, l);
    }

    while (l>1)
    {
        exch(arr[1], arr[l]);
        l--;
        fixDown(arr, 1, l);
    }
    return;
}
void HeapSort::Heapify(std::vector<int>& vector)
{
	for (int i = vector.size()-1; i >= 0; --i)
	{
		fixDown(vector, i, vector.size());
	}
}
Beispiel #5
0
int PQdelmin(){ 
  int tmp;
  tmp = pq[1];
  pq[1] = pq[N];
  pq[N] = tmp; 
  fixDown(pq, 1, N-1); 
  return pq[N--]; 
}
Node delMin (PriQ q) {
    Node n;
    n = q->heap[1];
    q->heap[1] = q->heap[q->numItems];
    fixDown(q->heap, 1, q->numItems);
    q->numItems--;
    return n;
}
void HeapSort::sort(std::vector<int>& vector)
{
	Heapify(vector);
	for (int i = vector.size()-1; i >= 0; --i)
	{
		int tmp;
		tmp = vector[i];
		vector[i] = vector[0];
		vector[0] = tmp;
		fixDown(vector, 0, i);
	}
}
Beispiel #8
0
//delete the item w/ the smallest key(freq) and return it
Item PQdelmin()
{
	
	int i;
	//exchange the top and bottom items
	exch(1, N);
	
	//restore the heap condition on the remaining items, reducing the size of the pq
	fixDown(1, --N);

	//return the item that is now outside the range of the pq
	return pq[N+1];

}
Beispiel #9
0
 Item getmax()
 {
     exch(pq[1], pq[N]);
     fixDown(pq, 1, N-1);
     return pq[N--];
 }
int pqueue::getmin() {
     exch(1, n);
     fixDown(1, n-1);
     return pq[n--];
}
Item PQdelmax(void)
{
  exch(pq[1], pq[N]);
  fixDown(pq, 1, N-1);
  return pq[N--];
}
Beispiel #12
0
int removeFila(double prty[]) { 
   exch(1, N); 
   --N; 
   fixDown( 1, prty); 
   return pq[N+1]; 
}
void
base_heapsort(unsigned int a[], int N)
{
	int k;

	int is_even = !(N & 1);

#ifdef _USE_SOFTWARE_PREDICTOR
	up_down0 = &global_predictor[0];
	up_down1 = &global_predictor[1];
	up_down2 = &global_predictor[2];
#endif

	/* we switch predictors after the creation, so that we can record both */
	describe_predictor(&global_predictor[0], "Creation, has children");
	describe_predictor(&global_predictor[1], "Creation, child comparison");
	describe_predictor(&global_predictor[2], "Creation, child promotion");
	describe_predictor(&global_predictor[3], "Destruction, has children");
	describe_predictor(&global_predictor[4], "Destruction, child comparison");
	describe_predictor(&global_predictor[5], "Destruction, child promotion");
	
	if (is_even)
	{
		/* dont use compexch so that its only used in quicksort and its easier
		 * to code the branch_taken/branch_not_taken code */
        if (a[(N-1)/2] < a[N-1])
		{
			unsigned int t = a[N-1];
			a[N-1] = a[(N-1)/2];
			a[(N-1)/2] = t;
		}
		N--;
	}

	/* construct the heap */ 
	for(k=N/2; k >= 1; k--)
	{
		fixDown(&a[-1], k, N); /* while pretending there are no parents with 1 child*/
	}

	if (is_even)
	{
		exch(a[0], a[N]); /* the final place */
		fixDown(&a[-1], 1, N); /* still pretending its not there */
	}

	/* destroy the heap for the sortdown */
#ifdef _USE_SOFTWARE_PREDICTOR
	up_down0 = &global_predictor[3];
	up_down0->state = global_predictor[0].state;
	up_down1 = &global_predictor[4];
	up_down1->state = global_predictor[1].state;
	up_down2 = &global_predictor[5];
	up_down2->state = global_predictor[2].state;
#endif
	/* we cant overwrite anything with anything from limits.h, because this is in place */
	while(N>8)
	{
		unsigned int temp = a[0]; /* this is the right child */
		a[0] = a[N-1]; /* move the value to be sifted down */
		fixDown(&a[-1], 1, N-1); /* here we pretend there are 2 children */
		a[N-1] = temp; /* write the high value in its place */

		/* now the left child */
		temp = a[0];
		a[0] = a[N-2];
		fixDown(&a[-1], 1, N-2); /* now it has no children, as in the original algorithm */
		a[N-2] = temp;

		/* unroll - this has little effect ~= .1 percent*/
		temp = a[0];
		a[0] = a[N-3];
		fixDown(&a[-1], 1, N-3);
		a[N-3] = temp;

		temp = a[0];
		a[0] = a[N-4];
		fixDown(&a[-1], 1, N-4);
		a[N-4] = temp;

		temp = a[0];
		a[0] = a[N-5];
		fixDown(&a[-1], 1, N-5);
		a[N-5] = temp;

		temp = a[0];
		a[0] = a[N-6];
		fixDown(&a[-1], 1, N-6);
		a[N-6] = temp;

		temp = a[0];
		a[0] = a[N-7];
		fixDown(&a[-1], 1, N-7);
		a[N-7] = temp;

		temp = a[0];
		a[0] = a[N-8];
		fixDown(&a[-1], 1, N-8);
		a[N-8] = temp;


		N -= 8;
	}

	while(N>1)
	{
		unsigned int temp = a[0]; 
		N--;
		a[0] = a[N]; 
		fixDown(&a[-1], 1, N); 
		a[N] = temp; 
	}
}