Ejemplo n.º 1
0
void heapSort(int arr[],int size)
{
buildMaxHeap(arr,size);
int h = size;
for(int i = size-1 ; i >0; i--)
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
h= h-1;
maxHeapify(arr,h,0);
}
}
Ejemplo n.º 2
0
int heapExtractMax()
{
	assert(heap_size>=1);

	int max = number[1];

	//move the last value to root, then adjust to heap
	number[1] = number[heap_size];
	heap_size --;
	maxHeapify(1);

	return max;
}
Ejemplo n.º 3
0
	void PQueue<ElemType>::maxHeapify(ElemType arr[], int parent, int size) {
		int lchild = 2 * (parent + 1) - 1;
		int	largest, rchild = lchild + 1;
		if (lchild < size && cmp(arr[lchild], arr[parent]) > 0)
			largest = lchild;
		else largest = parent;
		if (rchild < size && cmp(arr[rchild], arr[largest]) > 0)
			largest = rchild;
		if (largest != parent) {
			swap(arr[largest], arr[parent]);
			maxHeapify(arr, largest, size);
		}
	}
Ejemplo n.º 4
0
// A utility function to create a max heap of given capacity
struct MaxHeap* createAndBuildHeap(int *array, int size)
{
    int i;
    struct MaxHeap* maxHeap = (struct MaxHeap*) malloc(sizeof(struct MaxHeap));
    maxHeap->size = size;   // initialize size of heap
    maxHeap->array = array; // Assign address of first element of array
 
    // Start from bottommost and rightmost internal mode and heapify all
    // internal modes in bottom up way
    for (i = (maxHeap->size - 2) / 2; i >= 0; --i)
        maxHeapify(maxHeap, i);
    return maxHeap;
}
Ejemplo n.º 5
0
void HeapSort::maxHeapSort( int* array, int arraySize )
{
	buildMaxHeap(array, arraySize);

	for (int i = arraySize - 1; i > 0; --i)
	{
		int tmp = array[i];
		array[i] = array[0];
		array[0] = tmp;
		++mCost;++mCost;
		maxHeapify(array, i , 0);
	}
}
 // Adds a number into the data structure.
 void addNum(int num) {
     if(maxHeap.size() == 0 && minHeap.size() == 0) {
         maxHeap.push_back(num);
         return;
     }// if first num to add
     if(maxHeap.size() <= minHeap.size()){
         if(num > minHeap[0]){
             minHeap.push_back(num);
             minHeapify();
             maxHeap.push_back(extractMin());
             maxHeapify();
             //return;
         }else{
             maxHeap.push_back(num);
             maxHeapify();
             //return;
         }
     }else{
         if(num < maxHeap[0]){
             maxHeap.push_back(num);
             maxHeapify();
             minHeap.push_back(extractMax());
             minHeapify();
             //return;
         }else{
             minHeap.push_back(num);
             minHeapify();
             //return;
         }
     }
     if(maxHeap.size()>minHeap.size() + 1){
         minHeap.push_back(extractMax());
         minHeapify();
     }else if(minHeap.size()>maxHeap.size()){
         maxHeap.push_back(extractMin());
         maxHeapify();
     }
 }
Ejemplo n.º 7
0
void heapSort() { 
    buildMaxHeap();
	printf("\ncreate heap:"); 
	printHeap();
	
	int heap_size_ = heap_size;
	for(int i=heap_size; i>=2; i--)
	{
		std::swap(number[1], number[i]);
		heap_size--;
		maxHeapify(1);
	}
	heap_size = heap_size_;
} 
Ejemplo n.º 8
0
void heapSort(long a[], long n) {
	long i;
	long t;
		
		buildMaxHeap(a, n);
		for (i = n - 1; i > 0; i--) {
			// a[0] is always the maximum. 
			t = a[i];
			a[i] = a[0];
			a[0] = t;
			maxHeapify(a, 0, i);
		}
		return;
    }
Ejemplo n.º 9
0
void maxHeapify(Vector& data, unsigned i, unsigned len)
{
  unsigned left = childLeft(i);
  unsigned right = childRight(i);

  unsigned largest = (left < len && (data[left] > data[i])) ? left : i;
  if(right < len && (data[right] > data[largest]))
    largest = right;

  if(largest != i)
  {
    std::swap(data[i], data[largest]);
    maxHeapify(data, largest, len);
  }
}
Ejemplo n.º 10
0
void buildMaxHeap(int A[], int length)
{
    // The array A[] is the array representation of a heap.
    // A heap is a nearly complete binary tree. Therefore
    // the elements in the last array are located at
    // floor(length/2) + 1, floor(length/2) + 2 and so on.
    int i;
    int heapSize = length;

    // Loop invariant: At the beginning of every loop, the
    // left and right childrean are roots of max heaps.
    for (i = length/2; i >= 0; i--) {
        maxHeapify(A, i, heapSize);
    }
}
Ejemplo n.º 11
0
void maxHeapify(int *heap,int heapSize, int i){
	int left = LEFT(i), right = RIGHT(i), largest;
	
	if(left < heapSize && heap[i] < heap[left]) 
		largest = left;
	else 
		largest = i;
	
	if(right < heapSize && heap[largest] < heap[right]) 
		largest = right;
	
	if (largest  != i) {
		exchange(heap, i, largest);
		maxHeapify(heap, heapSize, largest);
	} 
}
Ejemplo n.º 12
0
void printMaxK(int arr[],int size,int k)
{
int h = size;
buildMaxHeap(arr,size);
for(int i = 0; i < k; i++)
{
printf("%d\t",arr[0]);
int temp = arr[0];
arr[0]= arr[h];
arr[h]= temp;
h = h-1;
maxHeapify(arr,h,0);
}


}
Ejemplo n.º 13
0
void maxHeapify(int A[],int n, int i) {
    int left = 2 * i + 1;
    int right = 2 * i + 2;
    int largest = i;
    if (left < n && A[left] > A[largest]) {
        largest = left;
    }
    if (right < n && A[right] > A[largest]) {
        largest = right;
    }

    if (largest != i) {
        std::swap(A[largest], A[i]);
        maxHeapify(A, n, largest);
    }
}
int maxHeapify(int *S, int i)
{
   int l = 2*i;
   int r = 2*i + 1;
   int largest = 0;
   if(l <= S[0] && S[l] > S[i]) // S[0] store length of heap
      largest = l;
   else
      largest = i;
   if(r <= S[0] && S[r] > S[largest])
      largest = r;
   if(largest != i) {
      exchange(S + i, S + largest);
      maxHeapify(S, largest);
   }
   return 0;
}
Ejemplo n.º 15
0
void p_queue::maxHeapify(int A[], int n, int i)
{
    int l = left(i);
    int r = right(i);
    int largest(0);
    if (l <= (n-1) && A[l] > A[i])
        largest = l;
    else
        largest = i;
    if (r <= (n-1) && A[r] > A[largest])
        largest = r;
    if(largest != i)
    {
        std::swap(A[i], A[largest]);
        maxHeapify(A, n, largest);
    }
}
Ejemplo n.º 16
0
/** Inserts a num into the data structure. left of median=maxheap right of median=minheap*/
void addNum(struct MedianFinder* mf, int num) {
  if(!mf)
    return;
  int i = compare(mf->maxheap->count, mf->minheap->count);
  printf("\n median=%lf, num=%d",mf->median,num);
  switch(i)
  {
    case -1:
      if(num<mf->median)
        insertMaxHeap(mf->maxheap, num);
      else{
        int e = mf->minheap->arr[0];
        insertMaxHeap(mf->maxheap,e);
        mf->minheap->arr[0]=num;
        minHeapify(mf->minheap, 0);
      }
      mf->median=(double)((mf->maxheap->arr[0]+mf->minheap->arr[0])/2);
      break;
    case 0:
      if(num<mf->median)
      {
        insertMaxHeap(mf->maxheap, num);
        mf->median= mf->maxheap->arr[0];
      }
      else{
        insertMinHeap(mf->minheap, num);
        mf->median= mf->minheap->arr[0];
      }
      break;
    case 1:
      if(num<mf->median){
        int e = mf->maxheap->arr[0];
        insertMinHeap(mf->minheap,e);
        mf->maxheap->arr[0]=num;
        maxHeapify(mf->maxheap, 0);
      }
      else{
        insertMinHeap(mf->minheap, num);
      }
      mf->median=(mf->maxheap->arr[0]+mf->minheap->arr[0])/2;
      break;
    default:
      break;
  }
}
Ejemplo n.º 17
0
void maxHeapify(Heap *h, int idx)
{
  if(!h)
    return;
  int left = 2*idx+1;
  int right = 2*idx+2;
  int largest =idx;
  if(left<h->count && h->arr[idx]<h->arr[left])
    largest=left;
  if(right < h->count && h->arr[largest] < h->arr[right])
    largest=right;
  if(largest!=idx)
  {
    swap(&h->arr[largest], &h->arr[idx]);
    maxHeapify(h,largest);
  }

}
Ejemplo n.º 18
0
void maxHeapify(int i)
{
	int l = left(i);
	int r = right(i);
	int largest;
	if(l <= heapsize && arr[l]>arr[i])
		largest = l;
	else largest = i;
	if(r<= heapsize && arr[r]>arr[largest])
		largest=r;
	if(largest!=i)
	{
		int temp = arr[i];
		arr[i] = arr[largest];
		arr[largest] = temp;
		maxHeapify(largest);
	}
}
Ejemplo n.º 19
0
void maxHeapify(Array &a, int index)
{
    int l = LEFT(index);
    int r = RIGHT(index);
    int largest = 0;
    if (l <= a.length && a[l] > a[index]) {
       largest = l; 
    }else{
        largest = index;
    }
    if (r <= a.length && a[r] > a[largest]) {
       largest = r; 
    }
    if (largest != index) {
       exchange(a[index], a[largest]); 
       maxHeapify(a, largest);
    }
}
Ejemplo n.º 20
0
// The main function to sort an array of given size
void heapSort(int* array, int size)
{
  // Build a heap from the input data.
  struct MaxHeap* maxHeap = createAndBuildHeap(array, size);
  
  // Repeat following steps while heap size is greater than 1.
  // The last element in max heap will be the minimum element
  while (maxHeap->size > 1) {
    // The largest item in Heap is stored at the root. Replace
    // it with the last item of the heap followed by reducing the
    // size of heap by 1.
    swap(&maxHeap->array[0], &maxHeap->array[maxHeap->size - 1]);
    --maxHeap->size;  // Reduce heap size
    
    // Finally, heapify the root of tree.
    maxHeapify(maxHeap, 0);
  }
}
Ejemplo n.º 21
0
struct heap* heapSort(struct heap* H){
//sorting is in increasing value of key
	H=buildMaxHeap(H);
	int i,n;
	int *t1,*t2;
	n=H->heapSize;

	for(i=n-1;i>=1;i--){
		t1=&(H->A[0]);
		t2=&(H->A[i]);
		swap(t1,t2);
		H->heapSize--;//reduce the size. the last element shouldn't take part in heapify
		maxHeapify(H,0);
	}//end of for loop
//restoring heapSize
H->heapSize=n;

}//end of heapSort()
Ejemplo n.º 22
0
void buildHeap(int A[], int p, int q, int flag)
{
    int i;
    if(flag)
    {
        for(i = p+(q-p+2)/2; i >= p; i--)
        {
    	    minHeapify(A, p, q, i);
        }
    }
    else
    {
        for(i = p+(q-p+2)/2; i >= p; i--)
        {
    	    maxHeapify(A, p, q, i);
        }
    }
    return;
}
Ejemplo n.º 23
0
void maxHeapify (int A[], int p, int q, int i)
{
    int l, r;
    int temp;
    l = left(i, p);
    r = right(i, p);
    int largest = i;
    if((l <= q) && (A[l] > A[i]))
	    largest = l;
    if((r <= q) && (A[r] > A[largest]))
	    largest = r;
    if(largest != i)
    {   
        temp = A[largest];
	    A[largest] = A[i];
	    A[i] = temp;
	    maxHeapify(A, p, q, largest);
    }
}
Ejemplo n.º 24
0
void HeapSort::maxHeapify( int* heap, int heapSize, int parent )
{
	int largest;
	if (_left(parent) < heapSize && heap[_left(parent)] > heap[parent])
		largest = _left(parent);
	else largest = parent;

	if (_right(parent) < heapSize && heap[_right(parent)] > heap[largest])
		largest = _right(parent);

	if (largest != parent)
	{
		int tmp = heap[parent];
		heap[parent] = heap[largest];
		heap[largest] = tmp;
		++mCost;++mCost;
		maxHeapify(heap, heapSize, largest);
	}
}
Ejemplo n.º 25
0
void maxHeapify(struct MaxHeap *maxHeap, int idx)
{
    int largest = idx;   // Initialize largest as root
    int left = (idx << 1 ) + 1; // left = 2*idx + 1
    int right = (idx + 1 ) << 1;    // right = 2*idx + 2
    
    if (left < maxHeap->size &&
        maxHeap->array[left] > maxHeap->array[largest])
        largest = left;
    
    if(right < maxHeap->size &&
        maxHeap->array[right] > maxHeap->array[largest])
        largest = right;
        
    if(largest != idx)
    {
        swap(&maxHeap->array[largest], &maxHeap->array[idx]);
        maxHeapify(maxHeap, largest);
    }
}
Ejemplo n.º 26
0
int heapExtractMax(struct heap* H){
	if(H->flag!=1){
	printf("Extract max function is only supported in a max heap\n");
	return -1;
	}

	int max;
	// Input: A: an array representing a heap
	// Output: The maximum element of A and A as a heap with this element removed
	// Running Time: O(log n) where n =heap-size[A]
	max=H->A[0];
	int *t1,*t2;
	t1=&(H->A[0]);
	t2=&(H->A[H->heapSize-1]); //replacing with last index
	swap(t1,t2);
	H->heapSize=H->heapSize-1;

	maxHeapify(H,0);
	return max;
}//end of heapExtractMax()
Ejemplo n.º 27
0
// The main function to heapify a Max Heap. The function assumes that 
// everything under given root (element at index idx) is already heapified
void maxHeapify(struct MaxHeap* maxHeap, int idx)
{
    int largest = idx;  // Initialize largest as root
    int left = (idx << 1) + 1;  // left = 2*idx + 1
    int right = (idx + 1) << 1; // right = 2*idx + 2
 
    // See if left child of root exists and is greater than root
    if (left < maxHeap->size && maxHeap->array[left] > maxHeap->array[largest])
        largest = left;
 
    // See if right child of root exists and is greater than the largest so far
    if (right < maxHeap->size && maxHeap->array[right] > maxHeap->array[largest])
        largest = right;
 
    // Change root, if needed
    if (largest != idx)
    {
        swap(&maxHeap->array[largest], &maxHeap->array[idx]);
        maxHeapify(maxHeap, largest);
    }
}
Ejemplo n.º 28
0
void Heap::maxHeapify(int i)
{
  int l = left(i);
  int r = right(i);
  int largest;
  if (l <= size && KEY(buf[l]) > KEY(buf[i]))
    largest = l;
  else
    largest = i;
  if (r <= size && KEY(buf[r]) > KEY(buf[largest]))
    largest = r;
  if (largest != i)
    {
      Page *temp = buf[i];
      buf[i] = buf[largest];
      buf[largest] = temp;
      UPDATE_INDEX(i);
      UPDATE_INDEX(largest);
      maxHeapify(largest);
    }
}
Ejemplo n.º 29
0
void maxHeapify(struct heap *H, int i){

	int l=left(i);
	int r=right(i);
	int largest;
	int *t1,*t2;

	if(l<=H->heapSize-1 && (H->A[l]> H->A[i]))
		largest=l;
	else
		largest=i;

	if(r<=H->heapSize-1 && (H->A[r]> H->A[largest]))
		largest=r;

	if(largest!=i){
		t1=&(H->A[i]);
		t2=&(H->A[largest]);
		swap(t1,t2);
		maxHeapify(H,largest);
		}
}//end of maxHeapify
void maxHeapify(int i)
{
  int l = d*i;
  int rshift = -(d-2);
  int largest = i;
  int count = 0;
  while(count<d)
  {
    if(l + rshift <=heap.length && heap.Arr[l+rshift]>heap.Arr[largest])
      largest = l + rshift;
    rshift++;
    count++;
  }
  
  if (largest !=i)
  {
    int temp = heap.Arr[i];
    heap.Arr[i] = heap.Arr[largest];
    heap.Arr[largest] = temp;
    maxHeapify(largest);
  }
}