1007 DNA Sorting

#include <stdio.h>

#define MAX_CHARS 50
#define MAX_STRINGS 100

struct DNA {
    char str[MAX_CHARS];
    int unsortedness;
};

int main() {
    struct DNA dnas[MAX_STRINGS];

    int n, m;//n=length m=number
    scanf("%d %d", &n, &m);

    int count = 0;
    while (count < m) {

        dnas[count].unsortedness = 0;
        scanf("%s", dnas[count].str);

        // compute the unsortedness
        for (int i = 0; i < n-1; i ++) {
            for (int j = i+1; j < n; j ++) {
                if (dnas[count].str[i]>dnas[count].str[j]) {
                    dnas[count].unsortedness ++;
                }
            }
        }

        count ++;

    }

    // create pointer array
    struct DNA *dnasp[MAX_STRINGS];
    for (int i = 0; i < m; i ++) {
        dnasp[i] = &dnas[i];
    }

    // sort
    for (int i = 1; i < m; i ++) {

        int j = i - 1;
        struct DNA *temp = dnasp[i];

        while (j >= 0 && dnasp[j]->unsortedness > temp->unsortedness) {
            dnasp[j+1] = dnasp[j];
            j --;
        }
        dnasp[j+1] = temp;
    }

    for (int i = 0; i < m; i ++) {
        printf("%s\n", dnasp[i]->str);
    }

    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容