MOOC Zhejiang University Data Structure-09-Sort 2 Insert or Merge (25 puntos)

Consulte 09-Sort 2 Insert or Merge (25 puntos)
Idea:
La ordenación de fusión aquí no puede usar recursividad (no sé cómo escribir recursividad), y la parte de la matriz ordenada después de cada ronda del bucle y la pregunta Se compara la segunda matriz (almacenada en la matriz B en este código). Si son iguales, use continue y flag, loop de nuevo y salga, e imprima el resultado.
Tenga en cuenta que, aunque la ordenación por combinación aquí utiliza un método no recursivo, todavía necesita rebobinar la matriz tempA a A, por lo que puede ser más fácil escribir el código más adelante para comparar si es igual que B. (Opinión personal)
Error:

  1. Desgarrar -; reducido una vez más
  2. int * tempA; // mal usado int tempA [MaxSize];
    tempA = (int *) malloc (n * sizeof (int));
    aquí olvidé que el nombre de la matriz es una constante de puntero y no se puede cambiar, por lo que debe definir una variable de puntero y no una matriz
  3. Este no es un error. En la función merge_pass, no escribí el siguiente código. En realidad, no necesito escribirlo. La razón: si el último segmento corto restante es menor que la longitud len, déjelo en la matriz A, porque de lo contrario merge1 La función también vuelve a colocar la matriz tempA en A, por lo que este paso se puede omitir (comprensión personal)
      else  //最后只剩1个子列
	 	for( j = i; j < n; j++ )
	 		tempA[j] = A[j];

El código general es el siguiente

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 105
int A[MaxSize],CopyA[MaxSize],B[MaxSize];
int IsInsertFlag=0;
int IsIdentical(int A[],int n){
    
    
    int i;
    for(i=0;i<n;++i){
    
    
        if(A[i]!=B[i]) return 0; 
    }
    return 1;
     
}
void Insert_sort(int A[],int n){
    
    
    int i,j,flag=0,temp;
    for(i=1;i<n;++i){
    
    
        temp = A[i];
        for(j=i;j>0&&A[j-1]>temp;--j){
    
    
            A[j] = A[j-1];
        }
        A[j] = temp;
        if(IsIdentical(A,n)){
    
    
            printf("Insertion Sort\n");
            flag = 1;
            IsInsertFlag = 1;
            continue;
        }
        if(flag) break;
    }
}
void Merge1(int A[],int tempA[],int L,int R,int Rend){
    
    
    int Lend,temp;
    Lend = R-1;
    // len = Rend-L+1;
    temp = L;
    while(L<=Lend&&R<=Rend){
    
    
        if(A[L]<=A[R]){
    
    
            tempA[temp++] = A[L++];
        }
        else{
    
    
            tempA[temp++] = A[R++];
        }
    }
    while(L<=Lend){
    
    
        tempA[temp++] = A[L++];
    }
    while(R<=Rend){
    
    
        tempA[temp++] = A[R++];
    }
    for(;Rend>=0;Rend--){
    
    
        A[Rend] = tempA[Rend];
        // Rend--;
    }
}
void Merge_pass(int A[],int tempA[],int len,int n){
    
    
    int i,j;
    for(i=0;i+2*len<n;i+=2*len){
    
    
        Merge1(A,tempA,i,i+len,i+2*len-1);
    }
    if(i+len<n){
    
    
        Merge1(A,tempA,i,i+len,n-1);
    }
    // else  //最后只剩1个子列
	// 	for( j = i; j < n; j++ )
	// 		tempA[j] = A[j];
}
void Merge_sort(int A[],int n){
    
    
    int *tempA;//wrong used int tempA[MaxSize];
    tempA = (int*)malloc(n*sizeof(int));
    int len=1,flag = 0;
    if(tempA){
    
    
        while(len<n){
    
    
            Merge_pass(A,tempA,len,n);
            if(IsIdentical(CopyA,n)){
    
    
                printf("Merge Sort\n");
                flag = 1;
                len *= 2;
                continue;
            }
            len *= 2;
            if(flag) break;
        }
        free(tempA);
    } 
}
int main(){
    
    
    int n,i;
    scanf("%d",&n);
    for(i=0;i<n;++i){
    
    
        scanf("%d",&A[i]);
        CopyA[i] = A[i];
    }
    for(i=0;i<n;++i){
    
    
        scanf("%d",&B[i]);
    }
    Insert_sort(A,n);
    if(IsInsertFlag){
    
    
        printf("%d",A[0]);
        for(i=1;i<n;++i){
    
    
            printf(" %d",A[i]);
        }
    }
    else{
    
    
        Merge_sort(CopyA,n);
        printf("%d",CopyA[0]);
        for(i=1;i<n;++i){
    
    
            printf(" %d",CopyA[i]);
        }        
    }
    return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_43919570/article/details/105837255
Recomendado
Clasificación