【发布时间】:2017-03-11 15:02:36
【问题描述】:
我知道什么是插入排序,但我不懂代码。我搜索了所有关于排序的解释,而不是代码。如果您通过示例逐步回答问题,对我来说会更容易!谢谢! 例如,使用此代码。我将我的问题放在评论中以使其更清楚。
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
/* if arr[j] > key, at here arr[j+1] has the number in arr[j] which is i
* or key right? But now only arr[j+1] changed. There are only two same
* numbers which is the bigger number, arr[j] is not changed to the smaller
* number which arr[j+1] was.
*/
j = j-1;
}
arr[j+1] = key;
/* so at here arr[j+1] now has the value of key which is arr[i], so the
* sorted arr[j+1] ended up unchanged? I know this step is supposed to
* change key to use it to compare numbers after.
*/
}
}
【问题讨论】:
-
你可以很容易地用谷歌搜索这个
标签: c algorithm sorting insertion-sort insertion