これはスタックオーバーフローに関する私の最初の質問です。私はCS50を通してプログラミングの世界で私の旅を始めました。 Cの挿入ソート用の私のコードは、インストラクターの擬似コードが示唆したものとは少し異なります。私はちょうど次のコードが正しいと私はそれをより良くすることができますかどうかを知りたいですか?どうもありがとう。私の実装は正しく実装されていますか?
int arr[6] = {23, 42, 4, 16, 8, 15};
for (int i = 1; i < 6; i++) // Iterating over the unsorted portion of array.
{
int element = arr[i]; // This is the first element of the unsorted portion.
int temp = -1;
for(int j = i-1; j >= 0 && element < arr[j]; j--) // Iterating over the unsorted portion of array from right to left.
{
arr[j+1] = arr[j];
temp = j;
}
if(temp != -1) // If temp does not change, the element is already sorted.
{
arr[temp] = element;
}
}
@JonathanLeffler大変ありがとうございました。 – PseudoAficionado