简体   繁体   English

插入排序 C 编程

[英]Insertion Sort C programming

Since this year I'm starting studying C programming at university.从今年开始,我开始在大学学习 C 编程。 In particular today I was trying to understand the insertion sort.特别是今天我试图理解插入排序。 I wrote this code that is perfectly working:我编写了这段完美运行的代码:

void insertionSort (int v[], int s) 
{
    int i;
    int j;
    int value;

    for (i = 1; i < s; i++)
    {
        value = v[i];

        for (j = i - 1; (j >= 0) && (value < v[j]); j --)
        {
            v[j + 1] = v[j];           
        }
        v[j + 1] = value;               // why v[j+1]?
     }                          
}

My question is about the last code line: v[j + 1] = value .我的问题是关于最后一行代码: v[j + 1] = value If I understand correctly, j (that decreases every time), at the end of the for cycle, has a value of -1 and that's why is correct to write v[j + 1] = value .如果我理解正确, j (每次都会减小)在for循环结束时的值为 -1,这就是为什么写v[j + 1] = value是正确的。 Am I right or am I missing something?我是对的还是我错过了什么? Really thanks for anybody who wants to help me by explaining me better.真的感谢任何想通过更好地解释我来帮助我的人。

This is the process of Insertion Sort. 这是插入排序的过程。 It will swap if the numbers are not ordered. 如果未订购数字,它将交换。 插入排序

The way you have your code setup right now, you need v[j + 1] because j will always be one before where you want to insert. 现在,您设置代码的方式需要v [j + 1],因为j始终是您要插入的位置之前的1。

For example: 例如:

int v[6] = {1, 34, 2, 50, 4, 10} int v [6] = {1、34、2、50、4、10}

s = sizeof(v) / sizeof(v[0]) = 6 s = sizeof(v)/ sizeof(v [0])= 6

Stepping through your code: 逐步执行代码:

  • i = 1, j = 0 i = 1,j = 0
  • value = v[i] = 34 值= v [i] = 34
  • 34 < 1 is false so it doesn't go into the inner for loop 34 <1为假,因此它不会进入内部for循环
  • v[j + 1] = 34 which is right where 34 should be v [j + 1] = 34,正确的地方应该是34
  • Looping your entire code a second time: value = 2, j = 1, i = 2 第二次循环整个代码:value = 2,j = 1,i = 2
  • Both conditions are met where j = 1 && 2 < 34 and you go into your inner loop 当j = 1 && 2 <34时,两个条件都满足,并且进入内循环
  • Since you already stored v[2] earlier when you did value = v[i], v[2] = 34 at this point is where you decrease j by 1 making j = 0 由于在进行value = v [i]时,您之前已经存储了v [2],因此此时v [2] = 34就是将j减1使得j = 0

Looking at your array, it looks like this: 1, 34, 34 看一下数组,它看起来像这样:1,34,34

  • The inner for loop will try to loop again but fail the second check 内部的for循环将尝试再次循环,但第二次检查失败
  • At this point, j is 0 and when you do v[j + 1] = value, you're storing value (2) in its proper place. 此时,j为0,并且当您执行v [j + 1] = value时,您将值(2)存储在适当的位置。
  • Your array at this point looks like 1, 2, 34 此时的数组看起来像1,2,34

So again, the significance of v[j + 1] is to insert in the correct place. 同样,v [j + 1]的意义在于将其插入正确的位置。 If the value is already in the correct place than you swap with itself. 如果该值已经在正确的位置,则您将与其自身交换。

Over here you can find an visualized example: https://visualgo.net/en/sorting 在这里,您可以找到一个可视化的示例: https : //visualgo.net/en/sorting

Here you have an example in C: 这里有一个C语言示例:

#include <stdio.h>

int main()
{
  int n, array[1000], c, d, t;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }


  // Insertion Sort
  for (c = 1 ; c <= n - 1; c++) {
    d = c;

    while ( d > 0 && array[d] < array[d-1]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;

      d--;
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }

  return 0;
}

mark first element as sorted 将第一个元素标记为已排序

for each unsorted element 对于每个未分类的元素

'extract' the element “提取”元素

for i = lastSortedIndex to 0 对于i = lastSortedIndex为0

if currentSortedElement > extractedElement 如果currentSortedElement> extractElement

move sorted element to the right by 1 将排序的元素向右移动1

else: insert extracted element 其他:插入提取的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM