-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathПирамидальная сортировка1.cpp
104 lines (103 loc) · 1.53 KB
/
Пирамидальная сортировка1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
//#include <stack>
using namespace std;
struct Catridge
{
int k;
char r[10];
};
int main()
{
int b,c[2],i,j,l,l1,m,min,max,n,ob=0,r,r1,K,x;
char R[10];
Catridge *tab;
do
{
puts("Enter count of elements");
cin>>n;
}
while(n<=0);
tab=new Catridge[n];
for(i=0;i<n;i++)
{
cout<<"Enter key of element #"<<i+1<<"\n";
cin>>tab[i].k;
cout<<"Enter value of element #"<<i+1<<"\n";
cin>>tab[i].r;
}
l=n/2+1;
r=n-1;
while(r>0)
{
if(l>0)
{
l=l-1;
strcpy(R,tab[l].r);
K=tab[l].k;
}
else
{
strcpy(R,tab[r].r);
K=tab[r].k;
tab[r].k=tab[0].k;
strcpy(tab[r].r,tab[0].r);
r=r-1;
if(r==0)
{
tab[0].k=K;
strcpy(tab[0].r,R);
}
}
j=l;
do
{
i=j;
if(j>0)
{
j=2*j;
}
else
{
j=1;
}
if(j<=r)
{
if(j<r)
{
if(tab[j].k<tab[j+1].k)
{
j=j+1;
}
}
if(K<tab[j].k)
{
cout<<tab[i].k<<"\t"<<tab[j].k;
tab[i].k=tab[j].k;
strcpy(tab[i].r,tab[j].r);
//H4
}
}
}
while((K<tab[j].k)&&(j<=r));
tab[i].k=K;
strcpy(tab[i].r,R);
cout<<"\n"<<tab[i].k<<"\t"<<K<<"\n";
for(b=0;b<n;b++)
{
cout<<tab[b].k<<"\t"<<tab[b].r<<"\n";
}
cout<<"\n"<<"l="<<l;
cout<<"\n"<<"r="<<r<<"\n";
}
//cout<<"\n"<<"ob="<<ob<<"\n";
for(i=0;i<n;i++)
{
cout<<tab[i].k<<"\t"<<tab[i].r<<"\n";
}
getch();
}