6-9 Sort Three Distinct Keys (20分)

 

 

 

 

 解题思路:注意时间复杂度要求是O(n)

void MySort( ElementType A[], int N ) {
    int i;
    ElementType b[3]= {0};
    for(i=0; i<N; i++) {
        b[A[i]]++;
    }
    i=0;
    while(b[1]--)
        A[i++]=1;
    while(b[2]--)
        A[i++]=2;
    while(b[0]--)
        A[i++]=0;
}

 

posted @ 2020-04-21 15:52  跃鱼  阅读(258)  评论(0编辑  收藏  举报