-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathСортировка Бэтчера.cpp
103 lines (103 loc) · 1.62 KB
/
Сортировка Бэтчера.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;
struct Catridge
{
int k;
char r[10];
};
int get_t(int n)
{
int i=0,k=0,count=n,t;
while(count>1)
{
if(count%2!=0)
{
k=1;
}
count=count/2;
i++;
}
t=i+k;
cout<<"t= "<<t<<"\n";
return t;
};
int main()
{
int d,i,j=0,n,p,q,r,t,x,K;
char R[10];
Catridge *tab;
do
{
puts("Enter count of elements");
cin>>n;
}
while(n<=1);
tab=new Catridge[n];
t=get_t(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;
}
p=pow(2,t-1);
//p=2^(t-1);
//q=p/2;
//r=0;
//d=p;
while(p>0)
{
q=pow(2,t-1);
//cout<<"\n"<<"q= "<<q<<"\n";
r=0;
//cout<<"r= "<<r<<"\n";
d=p;
//cout<<"d= "<<d<<"\n";
do
{
//cout<<"n-d= "<<n-d<<"\n";
for(i=0;i<n-d;i++)
{
//cout<<"yes"<<"\n";
x=i&p;
//cout<<"x= "<<x<<"\n";
if(x==r)
{
//cout<<"yes"<<"\n";
if(tab[i].k>tab[i+d].k)
{
K=tab[i].k;
strcpy(R, tab[i].r);
tab[i].k=tab[i+d].k;
strcpy(tab[i].r, tab[i+d].r);
tab[i+d].k=K;
strcpy(tab[i+d].r, R);
j++;
//cout<<"j= "<<j<<"\n";
}
}
}
d=q-p;
q=q/2;
r=p;
//cout<<"\n"<<"q= "<<q<<"\n";
//cout<<"r= "<<r<<"\n";
//cout<<"d= "<<d<<"\n";
}
while(q>=p);
p=p/2;
//cout<<"\n"<<"p= "<<p<<"\n";
}
for(i=0;i<n;i++)
{
cout<<tab[i].k<<"\t"<<tab[i].r<<"\n";
}
delete[] tab;
getch();
}