- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
namespace belugina
{
void Simple_Merging_Sort (char *name){
int a1, a2, k, i, j, kol, tmp;
FILE *f, *f1, *f2;
kol = 0;
if ( (f = fopen(name,"r")) == NULL )
printf("\nИсходный файл не может быть прочитан...");
else { while ( !feof(f) ) {
fscanf(f,"%d",&a1);
kol++;
}
fclose(f);
}
k = 1;
while ( k < kol ){
f = fopen(name,"r");
f1 = fopen("smsort_1","w");
f2 = fopen("smsort_2","w");
if ( !feof(f) ) fscanf(f,"%d",&a1);
while ( !feof(f) ){
for ( i = 0; i < k && !feof(f) ; i++ ){
fprintf(f1,"%d ",a1); fscanf(f,"%d",&a1); }
for ( j = 0; j < k && !feof(f) ; j++ ){
fprintf(f2,"%d ",a1); fscanf(f,"%d",&a1);}
}
fclose(f2); fclose(f1); fclose(f);
f = fopen(name,"w"); f1 = fopen("smsort_1","r");
f2 = fopen("smsort_2","r");
if ( !feof(f1) ) fscanf(f1,"%d",&a1);
if ( !feof(f2) ) fscanf(f2,"%d",&a2);
while ( !feof(f1) && !feof(f2) ){
i = 0; j = 0;
while ( i < k && j < k && !feof(f1) && !feof(f2) ) {
if ( a1 < a2 ) {
fprintf(f,"%d ",a1); fscanf(f1,"%d",&a1);i++;}
else {
fprintf(f,"%d ",a2);fscanf(f2,"%d",&a2); j++;}
}
while ( i < k && !feof(f1) ) {
fprintf(f,"%d ",a1); fscanf(f1,"%d",&a1); i++;}
while ( j < k && !feof(f2) ) {
fprintf(f,"%d ",a2); fscanf(f2,"%d",&a2); j++;}
}
while ( !feof(f1) ) {
fprintf(f,"%d ",a1); fscanf(f1,"%d",&a1); }
while ( !feof(f2) ) {
fprintf(f,"%d ",a2); fscanf(f2,"%d",&a2); }
fclose(f2); fclose(f1); fclose(f); k *= 2;
}
remove("smsort_1"); remove("smsort_2");
}
}
Rooster 26.05.2019 10:25 # 0
PA3yMHblu_nemyx 26.05.2019 10:27 # 0
Rooster 26.05.2019 10:48 # 0
guest8 26.05.2019 11:35 # −999