見出し画像

C言語教室 第26回 - いろいろなソート(回答提出)

こちらの課題回答です。

課題
文字列の配列をバブルソートで並べ替える関数を書いてください。文字列の大小比較は strcmp を使ってください。なお、文字列全体を入れ替えながらソートすると大変なので文字列へのポインタの配列を用意して、これをソートするようにしてください。

思いっきりサボりまくって、課題記事のコードを流用し、ちょっとだけ修正して終わりました。文字列はどうしようかと悩みに悩んで(笑)、好きな小説からの引用です。古典の名作。

課題記事のコメントで遊月さんも仰っていましたけど、私が把握していたバブルソートもこれとは少し違います。それについてはまた改めて書いてみようかな。


コード

#include <stdio.h>

void bubble_sort(char* array[], unsigned int size) {
  int cnt = 0;
  for (int i = 0; i < size - 1; i++) {
    for (int j = 0; j < size - i - 1; j++) {
      if (strcmp(array[j], array[j + 1]) > 0) {
        char* temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
		cnt++;
      }
    }
  }
  printf("bubble_sort swaped %d\n", cnt);
  printf("\n");
}

char* JaneEyre[7];
int main() {
  unsigned int size = 7;

  bubble_sort(JaneEyre, size);

  for (int i = 0; i < size; i++) 
  {
    printf("%s ", JaneEyre[i]);
 	printf("\n");
 	printf("\n");
  }

  return 0;
}

char* JaneEyre[7] =
{
"Do you think I can stay to become nothing to you?",

"Do you think I am an automaton? a machine without feelings? and can bear to have my morsel of bread snatched from my lips, and my drop of living water dashed from my cup?",

"Do you think, because I am poor, obscure, plain, and little, I am soulless and heartless?",

"You think wrong!",

"I have as much soul as you, and full as much heart!",

"And if God had gifted me with some beauty and much wealth, I should have made it as hard for you to leave me, as it is now for me to leave you.",

"I am not talking to you now through the medium of custom, conventionalities, nor even of mortal flesh; it is my spirit that addresses your spirit; just as if both had passed through the grave, and we stood at God's feet, equal, as we are!",
};

実行結果

bubble_sort swaped 9

And if God had gifted me with some beauty and much wealth, I should have made it as hard for you to leave me, as it is now for me to leave you. 

Do you think I am an automaton? a machine without feelings? and can bear to have my morsel of bread snatched from my lips, and my drop of living water dashed from my cup? 

Do you think I can stay to become nothing to you? 

Do you think, because I am poor, obscure, plain, and little, I am soulless and heartless? 

I am not talking to you now through the medium of custom, conventionalities, nor even of mortal flesh; it is my spirit that addresses your spirit; just as if both had passed through the grave, and we stood at God's feet, equal, as we are! 

I have as much soul as you, and full as much heart! 

You think wrong! 

蛇足

最後に。

引用した文章です。

"Do you think I can stay to become nothing to you? Do you think I am an automaton? — a machine without feelings? and can bear to have my morsel of bread snatched from my lips, and my drop of living water dashed from my cup? Do you think, because I am poor, obscure, plain, and little, I am soulless and heartless? You think wrong! — I have as much soul as you, — and full as much heart! And if God had gifted me with some beauty and much wealth, I should have made it as hard for you to leave me, as it is now for me to leave you. I am not talking to you now through the medium of custom, conventionalities, nor even of mortal flesh; — it is my spirit that addresses your spirit; just as if both had passed through the grave, and we stood at God's feet, equal, — as we are!"

そして、吉田健一訳。
これが好きで好きで(笑)。

あなたにとってなんでもなくなっても、まだここにいられるとお思いになるんですか。私が感情のない機械仕掛けの人形だと思っていらっしゃるんですか。私の心の糧を奪われても私が平気でいられると思っていらっしゃるんですか。私が貧乏で身分が低くて器量が悪くて小さな体をしているから、心も魂もないと思っていらっしゃるんですか。そんなことはありません。私にはあなたと同じだけ心も魂もあって、もし私に美しさと財産があったならば私がいまあなたにお別れするのが辛いのにおとらないくらい、あなたに私と別れるのが辛くさせてあげるんです。いま私が話しているのは世の中の習慣や仕来りに従ってではなく、私の肉体が話しているのでもなく、私の魂があなたの魂に話しているんです。死ねば私たちは神の前で平等な二人であるはずです。


この記事が気に入ったらサポートをしてみませんか?