#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;
}
网友评论