MOOC浙江大学のデータ構造-09-並べ替え1並べ替え(25ポイント)

並べ替えの演習では、バブルソート、挿入ソート、ヒルソート、ヒープソート、マージソート(再帰的および非再帰的)
エラーが記述されています

  1. for(j = i-1; j> = 0 && temp <a [j]; – j){// j> 0を忘れるループ制御変数が減少しているループの場合、条件としてj> 0を使用することを忘れないでください
  2. For(int i = 0; i <N; ++ i)ループで定義された変数は、ループを終了した後も使用を継続する方法がないようです
  3. // else()の後に括弧なしで間違って使用されたelse()
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100005
void insert_sort(int a[],int N);
void swap(int *a,int *b);
void bubble_sort(int a[],int N);
void shell_sort(int a[],int N);
void HeapAdjust(int H[],int f,int l);
void heap_sort(int a[],int N);
void Merge(int A[],int tempA[],int L,int R,int Rend);
// void Msort(int A[],int tempA[],int L,int R);
void Merge_sort(int A[],int N);
void Merge_pass(int A[],int tempA[],int len,int N);
int main(){
    
    
    int N;
    scanf("%d",&N);
    int i;
    int a[100005] = {
    
    0};
    int temp;
    for(i=0;i<N;++i){
    
    
        scanf("%d",&temp);
        a[i] = temp;
    }
    // bubble_sort(a,N);
    // insert_sort(a,N);
    // shell_sort(a,N);
    // heap_sort(a,N);
    Merge_sort(a,N);
    for(i=0;i<N;++i){
    
    
        if(i){
    
    
            printf(" ");
        }
        printf("%d",a[i]);
    }
}
void swap(int *a,int *b){
    
    
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
void bubble_sort(int a[],int N){
    
    
    int i,n,flag;
    for(n=N-1;n>0;n--){
    
    
        flag = 0;
        for(i=0;i<n;i++){
    
    
            if(a[i]>a[i+1]){
    
    
                swap(&a[i],&a[i+1]);
                flag = 1;
            }
        }
        if(flag==0) break;
    }
}
void insert_sort(int a[],int N){
    
    
    int i,j,temp;
    for(i=1;i<N;++i){
    
    
        temp = a[i];
        for(j=i-1;j>=0&&temp<a[j];--j){
    
    //forget j>0
            a[j+1] = a[j];
        }
        a[j+1] = temp;
    }
}
void shell_sort(int a[],int N){
    
    
    int i,j,temp,D;
    for(D=N/2;D>=1;D/=2){
    
    
        for(i=D;i<N;i+=D){
    
    
            temp = a[i];
            for(j=i-D;j>=0&&temp<a[j];j-=D){
    
    //forget j>0
                a[j+D] = a[j];
            }
            a[j+D] = temp;
        }
    }
}

void HeapAdjust(int H[],int f,int e){
    
    
    int temp = H[f];
    int parent,child;
    for(parent=f;parent*2+1<=e;parent=child){
    
    //在这里定义的i在循环外不能使用
        child = 2*parent+1;
        if(child+1<e&&H[child+1]>H[child]){
    
    
            child++;
        }
        if(H[child]<temp) break;
        else{
    
    
            H[parent] = H[child];
        }
    }
    H[parent]=temp;
}
void heap_sort(int a[],int N){
    
    
    int i=N/2-1;
    for(;i>=0;--i){
    
    
        HeapAdjust(a,i,N);
    }
    for(int j=N-1;j>=0;--j){
    
    
        swap(&a[0],&a[j]);
        HeapAdjust(a,0,j);
    }
}
void Merge(int A[],int tempA[],int L,int R,int Rend){
    
    
    if (L>=Rend) return;
    int count = Rend-L+1;
    int temp = L;
    int Lend = R-1;
    while(L<=Lend&&R<=Rend){
    
    
        if(A[L]>=A[R]) tempA[temp++] = A[R++];
        else tempA[temp++] = A[L++];
    }
    while(R<=Rend){
    
    
        tempA[temp++] = A[R++];
    }
    while(L<=Lend){
    
    
        tempA[temp++] = A[L++];
    }
}
void Merge_pass(int A[],int tempA[],int len,int N){
    
    
    int i;
    for(i=0;i+2*len<N;i+=2*len){
    
    
        Merge(A,tempA,i,i+len,i+2*len-1);
    }
    if(i+len<N){
    
    
        Merge(A,tempA,i,i+len,N-1);
    }
    else{
    
    
        for(;i<N;++i){
    
    
            tempA[i] = A[i];
        }       
    }
}
void Merge_sort(int A[],int N){
    
    
    int *tempA = (int*)malloc(N*sizeof(int));
    int len = 1;
    if(tempA){
    
    
        while(1){
    
    
            Merge_pass(A,tempA,len,N);
            len = len*2;
            Merge_pass(tempA,A,len,N);
            len = len*2;
            if(len>N) break;
        }
    free(tempA);//dont forget
    }

}


次のように再帰的にマージして並べ替えます。

void Merge(int A[],int tempA[],int L,int R,int Rend){
    
    
    if (L>=Rend) return;
    int count = Rend-L+1;
    int temp = L;
    int Lend = R-1;
    while(L<=Lend&&R<=Rend){
    
    
        if(A[L]>=A[R]) tempA[temp++] = A[R++];
        else tempA[temp++] = A[L++];
    }
    while(R<=Rend){
    
    
        tempA[temp++] = A[R++];
    }
    while(L<=Lend){
    
    
        tempA[temp++] = A[L++];
    }
    for(int i=0;i<count;i++){
    
    
        A[Rend] = tempA[Rend];//  wrong used  A[Rend--] = tempA[Rend];
        Rend--;
    }
}

void Msort(int A[],int tempA[],int L,int R){
    
    
    int cen;
    if(L<R){
    
    
        cen = (L+R)/2;
        Msort(A,tempA,L,cen);//wrong used Msort(A,tempA,0,cen);
        Msort(A,tempA,cen+1,R);
        Merge(A,tempA,L,cen+1,R);
    }
}
void Merge_sort(int A[],int N){
    
    
    int *tempA = (int*)malloc(N*sizeof(int));
    if(tempA){
    
    
        Msort(A,tempA,0,N-1);
        free(tempA);//dont forget
    }
    else{
    
    
        printf("空间不足");//wrong used else()
    }
}

文章はかなり乱雑です。実際、この記事は非常に明確です。並べ替えアルゴリズム-09-並べ替え1並べ替え(25ポイント)-パート1

おすすめ

転載: blog.csdn.net/weixin_43919570/article/details/105714653