-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathОбменно-поразрядная сортировка_Антикнут2.cpp
110 lines (105 loc) · 1.61 KB
/
Обменно-поразрядная сортировка_Антикнут2.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
105
106
107
108
109
110
#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];
};
void sort_bit(Catridge *a, int left, int right, int b)
{
int i,j,l,K,num_bit,r,x;
char R[10];
l=left;
r=right;
num_bit=b;
if(num_bit==0)
{
j=0;
for(i=l;i<=r;i++)
{
x=(1<<(32-b-1))&a[i].k;
//cout<<"x="<<x;
x=x>>(32-b-1);
cout<<"x="<<x;
if(x==-1)
{
K=a[i].k;
strcpy(R,a[i].r);
a[i].k=a[l+j].k;
strcpy(a[i].r, a[l+j].r);
a[l+j].k=K;
strcpy(a[l+j].r,R);
j++;
}
}
cout<<"yes";
}
else
{
j=0;
for(i=l;i<=r;i++)
{
x=(1<<(32-b-1))&a[i].k;
if(x==0)
{
K=a[i].k;
strcpy(R,a[i].r);
a[i].k=a[l+j].k;
strcpy(a[i].r, a[l+j].r);
a[l+j].k=K;
strcpy(a[l+j].r,R);
j++;
}
}
}
cout<<"l="<<l;
cout<<"r="<<r;
cout<<"b="<<num_bit;
if(num_bit<=31)
{
if((l>=0)&&(j>0))
{
sort_bit(a,l,l+j-1,num_bit+1);
}
if((r>=0)&&(r>=(l+j)))
{
sort_bit(a,l+j,r,num_bit+1);
}
}
}
int main()
{
int i,n;
//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;
}
//min=minmas(tab,n);
//max=maxmas(tab,n);
//l=0;
//r=n-1;
sort_bit(tab,0,n-1,0);
for(i=0;i<n;i++)
{
cout<<tab[i].k<<"\t"<<tab[i].r<<"\n";
}
getch();
}