C言語教室 第26回 - いろいろなソート(回答提出)
こちらの課題回答です。
思いっきりサボりまくって、課題記事のコードを流用し、ちょっとだけ修正して終わりました。文字列はどうしようかと悩みに悩んで(笑)、好きな小説からの引用です。古典の名作。
課題記事のコメントで遊月さんも仰っていましたけど、私が把握していたバブルソートもこれとは少し違います。それについてはまた改めて書いてみようかな。
コード
#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!
蛇足
最後に。
引用した文章です。
そして、吉田健一訳。
これが好きで好きで(笑)。