. :
1. a[] , .
2. k .
3. n .
void downHeap(int a[], long k, long n)
{
//
// : a[k+1]...a[n] -
// : a[k]...a[n] -
int new_elem;
long child;
new_elem = a[k];
while(k <= n/2) // a[k]
{
child = 2*k;
if(child < n && a[child] < a[child+1]) //
child++;
if(new_elem >= a[child])
break;
//
a[k] = a[child]; //
k = child;
}
a[k] = new_elem;
}
, .
void main()
{
int,key;
//
int *arr=new int [10];
//
cout<< Vvesti massiv. Otsortirovat' massiv pri pomoshi algoritma \n;
cout<< Piramidal'noy sortirivku.\n;
cout<<endl;
//
cout<<" Vibirite sposob vvoda:"<<endl;
cout<<endl;
cout<<"1 S klaviaturi"<<endl;
cout<<"2 Sluhaynie hisla"<<endl;
//
do{
cin>>key;
if(key==1||key==2) break;
else cout<<" Ne pravil'niy vvod. Povtorite"<<endl;
}while(1);
//
randomize();
//
switch(key){
case 1:
cout<<"Ishodniy massiv: "<<endl;
for (int i=0;i<10;i++)
{
//
cin>>arr[i];
//
if(cin.fail()){
do {
cout<<" Ne pravil'niy vvod. Povtorite"<<endl;
cin.clear();
cin.ignore();
cin>>arr[i];
}while(cin.fail());
}
}
cout<<endl;
break;
case 2:
cout<<"Sluchainiy massiv: "<<endl;
for (int i=0;i<10;i++)
{
//
arr[i]=random(20)-10;
//
cout<<arr[i]<<" ";
}
cout<<endl;
break;
}
heapSort(arr, size);
cout<<endl<<"Utogoviy massiv: "<<endl;
for (int i=0; i < 10; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
delete arr;
getch();
}