首页 > 技术文章 > 6-9 Sort Three Distinct Keys (20分)

snzhong 2020-04-21 15:52 原文

 

 

 

 

 解题思路:注意时间复杂度要求是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;
}

 

推荐阅读