Untitled
raw download clone
CPP
views 13
,
size 867 b
#include <iostream>
using namespace std;
void xuatmang(int arr[], int n){
	for(int i = 0; i < n; ++i){
		cout << arr[i] << " ";
	}
	cout << endl;
}
void heapfy(int arr[], int n, int i){
	int minest = i;
	int l = 2*i+1;
	int r = 2*i+2;
	
	if(l < n && arr[l]< arr[minest]){
		minest = l;
	}
	
	if(r < n && arr[r] < arr[minest]){
		minest = r;
	}
	
	if(minest != i){
		
		swap(arr[i], arr[minest]);
		
		heapfy(arr, n, minest);
	}
	xuatmang(arr,n);
}


void heap(int arr[], int n){
	
	for(int i = n/2-1; i >= 0; --i)
		heapfy(arr,n,i);
	xuatmang(arr,n);	
	for(int i = n-1; i>0; --i){
		swap(arr[0], arr[i]);
		xuatmang(arr,i);
		heapfy(arr, i, 0);
	}
}


int main(){
	int arr[] = {20, 15, 1, 30, 8, 9, 7, 3, 14, 6};
	int n = sizeof(arr) / sizeof(arr[0]);
	xuatmang(arr, n);
	heap(arr, n);
	xuatmang(arr,n);
	return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.