Exemple #1
0
int main(int argc, char * argv[]) {

    heap_t* h2 = makeSampleHeap(2);
    printHeap(h2, 2);
    buildMaxHeap(h2, 2);
    printf("< After Heapify >\n");
    printHeap(h2, 2);
    heapSort(h2, 2);
    printf("< After Heap Sort >\n");
    printHeap(h2, 2);
    releaseHeap(h2);
    printf("\n");

    heap_t* h3 = makeSampleHeap(3);
    printHeap(h3, 3);
    buildMaxHeap(h3, 3);
    printf("< After Heapify >\n");
    printHeap(h3, 3);
    printf("Extract Max = %d\n", heapExtractMax(h3));
    printf("< After Extracting Max >\n");
    printHeap(h3, 2);
    maxHeapInsert(h3, 8);
    printf("After Inserting key = %d >\n", 8);
    printHeap(h3, 3);
    releaseHeap(h3);
    printf("\n");

    heap_t* h4 = makeSampleHeap(4);
    printHeap(h4, 4);
    buildMaxHeap(h4, 4);
    printf("< After Heapify >\n");
    printHeap(h4, 4);
    heapSort(h4, 4);
    printf("< After Heap Sort >\n");
    printHeap(h4, 4);
    releaseHeap(h4);
    printf("\n");

    heap_t* h5 = makeSampleHeap(5);
    printHeap(h5, 5);
    buildMaxHeap(h5, 5);
    printf("< After Heapify >\n");
    printHeap(h5, 5);
    printf("Extract Max = %d\n", heapExtractMax(h5));
    printf("< After Extracting Max >\n");
    printHeap(h5, 4);
    maxHeapInsert(h5, 12);
    printf("< After Inserting key = %d >\n", 12);
    printHeap(h5, 5);
    releaseHeap(h5);
    printf("\n");

}
int main(void) { 
 //   srand(1); 
 //   
 //   //init the data
 //   int i;
 //   for(i = 1; i <= MAX; i++) { 
 //       number[i] = rand() % 100; 
 //   } 
	//heap_size = MAX;
	
	printf("test heapSort\n");
	int a[] = {4,1,3,2,16,9,10,14,8,7};
	heap_size = sizeof(a)/sizeof(a[0]);
	std::copy(a, a+heap_size,number+1);
	
	// print
	printf("before sort:"); 
	printHeap();
	
	// sort
    heapSort(); 
    printf("after sort"); 
	printHeap();

	printf("test priority queue\n");
	heap_size = sizeof(a)/sizeof(a[0]);
	std::copy(a, a+heap_size,number+1);

	buildMaxHeap();
	heapIncreaseKey( 9, 15 );
	printHeap();

    return 0; 
} 
Exemple #3
0
void testBuildMaxHeap() {
	int array1[] = {10,20,5};
	int array1C[] = {20,10,5};
	int array2[] = {6,10,5,3,7};
	int array2C[] = {10,7,5,3,6};

	printf("Test MaxHeapify\n");

	printf("Test 1\n");
	assert(memcmp(buildMaxHeap(array1, 3),array1C,sizeof(array1))==0);

	printf("Test 2\n");
	assert(memcmp(buildMaxHeap(array2, 5),array2C, sizeof(array2))==0);

	printf("Tests Passed\n");
}
 void heapSort(unsigned int size){
     buildMaxHeap(A, size);
     for (unsigned int i = size - 1; i > 0; i--){
         SWAP(A, i, 0);
         maxHeapify(0, i);
     }
 }
int main()
{
   int max = 0;
   heap[0] = 10;
   heap[1] = 1; heap[2] = 2; heap[3] = 3; heap[4] = 4;
   heap[5] = 7; heap[6] = 8; heap[7] = 9; heap[8] = 10;
   heap[9] = 14; heap[10] = 16;
  
   printf("Original State:\n");
   printHeap(heap);

   buildMaxHeap(heap);
   printf("\n After Heapifying:\n");
   printHeap(heap);

   heapIncreaseKey(heap, 9, 15);
   printf("\n After Increasing S[9]'s key to 15:\n");
   printHeap(heap);

   max = heapExtractMax(heap);
   printf("\n Max key is %d, After Extracting Max Key:\n", max);
   printHeap(heap);

   maxHeapInsert(heap, 13);
   printf("\n After inserting key 13:\n");
   printHeap(heap);

   return 0;
}
void maxHeapSort(int A[], int n) {
    buildMaxHeap(A, n);
    for ( int i = n-1; i > 0; i--) {
        std::swap(A[0], A[i]);
        maxHeapify(A, i, 0);
    }
}
void heapSort(int *heap, int heapSize){
	buildMaxHeap(heap, heapSize);
	int i = heapSize - 1;
	for(;i > 0; i--){
		exchange(heap, 0, i);
		heapSize--;
		maxHeapify(heap, heapSize, 0);
	}
}
Exemple #8
0
void p_queue::heapsort(int A[], int n)
{
    buildMaxHeap(A, n);
    for(int i = n-1;i > 0;i--)
    {
        std::swap(A[0],A[i]);
        maxHeapify(A, --n, 0);
    }
}
Exemple #9
0
p_queue::p_queue(int *A, int n)
{
    for(int i = 0;i < n;i++)
    {
        a[i] = A[i];
    }
    size = n;
    buildMaxHeap(a, size);
}
int main()
{
	scanf("%d",&dim);
	int i = 0;
	arr[0] = -1;
	for(i=1;i<=dim;i++)
		scanf("%d",&arr[i]);
	int mode = 0 ;	
	buildMaxHeap();
	print();
	while(mode!=4)
	{
		printf("\n1.Insert Into Heap\n2.Delete Max\n3.Find Max\n");
		scanf("%d",&mode);
		if(mode==1)
		{
			printf("\nEnter New Data\n");
			scanf("%d",&arr[dim+1]);
			dim++;
			buildMaxHeap();
			print();
		}
		else if(mode==2)
		{
			buildMaxHeap();
			for(i=1;i<dim;i++)
				arr[i] = arr[i+1];
			dim--;
			print();
		}
		else if(mode==3)
		{
			buildMaxHeap();
			printf("\n%d\n",arr[1]);
			print();
		}
		else
			break;
	}

	print();
	return 0;
}
int findKthMax(int a[], int n, int k){
    MaxHeap *heap = buildMaxHeap(a, n);
    
    int max;
    for (int i=0; i<k; i++) {
        max = deleteMax(heap);
        
    }
    
    return max;
}
Exemple #12
0
void heapsort(Array &a)
{
    int len = a.length;
    buildMaxHeap(a);
    for (int i = a.length; i >=2; i--)
    {
        exchange(a[1], a[a.length]);
        a.length--;
        maxHeapify(a, 1); 
    }
    a.length = len;
}
Exemple #13
0
struct heap *createAndBuildHeap(int *arr, int size)
{
	int i;
	struct heap *maxHeap = (struct heap *)malloc(sizeof(struct heap));
	maxHeap->size = size;
	maxHeap->arr = arr;
	for(i = (maxHeap->size - 2)/2; i >= 0; i--)
	{
		buildMaxHeap(maxHeap, i);
	}
	return maxHeap;
}
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);
	}
}
Exemple #15
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);
}
}
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;
    }
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_;
} 
int main()
{
  initializeHeap(heap);
  initializeValues();
  buildMaxHeap();
  printValues();
  //printf("max: %d \n",heapExtractMax());
  //heapIncreaseKey(2,18);
  //maxHeapInsert(17);
  printValues();


  return 0;
}
Exemple #19
0
void heapSort(int *arr, int size)
{
	struct heap *maxHeap = createAndBuildHeap(arr, size);

	while(maxHeap->size > 1)
	{
		int temp = maxHeap->arr[0];
		maxHeap->arr[0] = maxHeap->arr[maxHeap->size - 1];
		maxHeap->arr[maxHeap->size - 1] = temp;

		--maxHeap->size;

		buildMaxHeap(maxHeap, 0);
	}
}
Exemple #20
0
    void sort(T (&a)[N]) {
      buildMaxHeap(a);
      int n = N - 1;
      for(int i = n; i > 0; --i) {
        std::swap(a[0], a[i]);
        --n;
        siftDown(a, i);
      }
#ifdef LIBCXX
      int last = N;
      for (int i = last-1; i > 1; --last, --i) {
        pop(a, 0, last, i);
      };
#endif
    }
Exemple #21
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);
}


}
Exemple #22
0
// 希尔排序
void heapSort(int *data, int n) {
	unsigned long count = 0;
	clock_t start = clock();
	buildMaxHeap(data, n);
//	printArray(data, n);
//	printf("------------------------------------\n");
	int i;
	for (i = n; i > 1; i--) {
		// 删除到i=2时就可以了
		deleteMax(data, i);
		count++;
//		printf("[%d] ", n-i+1);
//		printArray(data, n);
	}
	timeinfo("heapSort", n, count, clock() - start);
}
Exemple #23
0
void buildMaxHeap(struct heap *maxHeap, int i)
{
	int largest = i; 
    int l = (i << 1) + 1; 
    int r = (i + 1) << 1; 

	if(l < maxHeap->size && maxHeap->arr[l] > maxHeap->arr[largest])
		largest = l;
	if(r < maxHeap->size && maxHeap->arr[r] > maxHeap->arr[largest])
		largest = r;
	if(largest != i)
	{
		int temp = maxHeap->arr[largest];
		maxHeap->arr[largest] = maxHeap->arr[i];
		maxHeap->arr[i] = temp;
		buildMaxHeap(maxHeap, largest);
	}	
}
Exemple #24
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()
Exemple #25
0
// heap sort 함수
void heapSort(heap_t* heap, int length) {

    int temp = 0;
    int i;

    if (!heap) return;

    buildMaxHeap(heap, length);

    for (i = length; i >= 2; i--) {
        temp = heap->element[i];
        heap->element[i] = heap->element[1];
        heap->element[1] = temp;

        heap->size--;

        max_heapify(heap, 1);
    }
}
Exemple #26
0
void heapSort(int* A, int n){
    /*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */
    /*  Inputs:                                                     */
    /*          A   :   int array   array of indices                */
    /*          n   :   int         number of element in array      */
    /*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */
    
    buildMaxHeap(A, n);
    
    int i;
    for(i = n - 1; i > 0; i--)
    {
        int temp = A[heapSize];
        A[heapSize] = A[0];
        A[0] = temp;
        heapSize--;
        minHeapify(A, 0);
    }
    
}
Exemple #27
0
int main() {

    int i = 1;
    int T[MAX];

    while(i < MAX+1) {
        T[i] = NIL;
        i++;
    }

    scanf("%d", &n);
    for(i = 1; i < n+1; i++) {
        scanf("%d", &T[i]);
    }
    buildMaxHeap(T);
    for(i = 1; i < n; i++) {
        printf(" %d", T[i]);
    }
    printf(" %d\n", T[n]);
    return 0;
}
Exemple #28
0
void heapsort(Vector& data)
{
  buildMaxHeap(data); // std::make_heap(data.begin(), data.end());
  sortHeap(data); // std::sort_heap(data.begin(), data.end());
}