Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';在c语言中使用插入排序无法理解一个点#_C#_Algorithm_Insertion Sort - Fatal编程技术网

C# Can';在c语言中使用插入排序无法理解一个点#

C# Can';在c语言中使用插入排序无法理解一个点#,c#,algorithm,insertion-sort,C#,Algorithm,Insertion Sort,这是一种插入排序算法 我理解将最大的“否”转发到下一个索引,但无法理解当它向前移动时,它的上一个位置(索引)是如何被刚刚比较过的较小数字占据的,例如在列表[2,1]2中,按列表[j+1]=列表[j]移动到下一个索引;但1如何向后移动或移动到上一个索引 //unsorted array int[] list = new int[] { 5, 2, 4, 6, 1 }; // the key element being sorted int key; // //star

这是一种插入排序算法

我理解将最大的“否”转发到下一个索引,但无法理解当它向前移动时,它的上一个位置(索引)是如何被刚刚比较过的较小数字占据的,例如在列表[2,1]2中,按列表[j+1]=列表[j]移动到下一个索引;但1如何向后移动或移动到上一个索引

 //unsorted array   
int[] list = new int[] { 5, 2, 4, 6, 1 };   

// the key element being sorted   
int key;   
// 
//start looping starting from the second element  
for (int i = 1; i < list.Length; i++)   
{  
    key = list[i];//store the key 
    int j = i - 1;//get the previous index  
    //
    //loop until you meet a smaller number or 0 
    while (j >= 0 && list[j] > key)  
    { 
        //move the greater number forward  
        list[j + 1] = list[j];  
        // Decrementing  
        j--;   
    }  
    //set the key in the proper index  
    list[j + 1] = key; 
}
//未排序的数组
int[]list=newint[]{5,2,4,6,1};
//正在排序的关键元素
int键;
// 
//从第二个元素开始循环
for(int i=1;i=0&&list[j]>键)
{ 
//向前移动较大的数字
列表[j+1]=列表[j];
//递减
j--;
}  
//在适当的索引中设置键
列表[j+1]=键;
}

在循环内最后一行中完成。向前移动一个或多个(甚至零个)项目后,当前值将置于正确位置

例如,如果您已将数组排序到最后一项,它如下所示:

2, 4, 5, 6, 1
将值1复制到
变量中,然后逐个向前复制项目:

2, 4, 5, 6, -   <-  the empty slot here still contains 1, but it's unused
2, 4, 5, -, 6   <-  the empty slot here still contains 6, but it's unused
2, 4, -, 5, 6
2, -, 4, 5, 6
-, 2, 4, 5, 6

插入排序背后的理论是从一个数组中获取项并插入到一个新数组中。您正在使用的实现仅使用单个数组,您可以将其视为分为已排序部分和未排序部分。排序后的零件以零尺寸开始:

[][ 5, 2, 4, 6, 1 ]
对数组进行排序时,将从未排序的零件中拾取项目,并将其插入到已排序零件的正确位置。已排序的零件将增长,未排序的零件将收缩,直到未排序的零件为空:

[ 5 ][ 2, 4, 6, 1 ]
[ 2, 5 ][ 4, 6, 1 ]
[ 2, 4, 5 ][ 6, 1 ]
[ 2, 4, 5, 6 ][ 1 ]
[ 1, 2, 4, 5, 6 ][]

你的问题的一些格式会很好。我修复了你的编辑。您需要去掉行号,并意识到SO中任何包含4个空格的内容都会变成代码。另外,前面的文本不需要是引用文本,答案在最后一行:
list[j+1]=key。在所有较高的数字向上移动后,将被测试的数字放置在正确的位置。如果一开始是在正确的位置,j将是
i-1
,因此
j+1
将是
i
。我是唯一一个听不懂任何由>4个单词组成的短语的人吗?请检查我选择的所有标记,如果你对c#插入排序有很好的了解,这并不难理解。它不是一个内置的列表数组只是我的数组的名称,我不明白你说的是什么这里有两个列表LIST i()和LIST j()它在最后一行完成了,正如Nikola所说的那样,这是一种插入排序,我刚刚使用了int数组名称列表,只有一个数组,因为j只是指向列表中的某个索引数组。你的答案有道理,但我仍然感到困惑,因为wn 6移动到最后一个索引,键值是4,它指向6,我不知道发生了什么接下来,你能举一个关于你上一次工作的例子吗sentence@Ted当前位置我不确定你的确切意思,但我想说得更清楚些,并添加了一些算法背后的理论。感谢你给了我正确的轨迹,它包含一个数组,看起来像被分成两部分,键在while循环的每次迭代中保留一个值,这就是我困惑的地方,对于案例2 3 4 1,它像这样迭代2 3 4,2 3 3 4,2 2 3 4当j值小于0时,j变为-1,则最后一个指令列表[j+1]=键,对于放置在j[0]处的最后一个索引为-1+1=0;请对我的问题投赞成票,这样我就可以获得15%的声誉,并对你的答案投赞成票。再次感谢,因为我正在准备考试。
[][ 5, 2, 4, 6, 1 ]
[ 5 ][ 2, 4, 6, 1 ]
[ 2, 5 ][ 4, 6, 1 ]
[ 2, 4, 5 ][ 6, 1 ]
[ 2, 4, 5, 6 ][ 1 ]
[ 1, 2, 4, 5, 6 ][]