c - 在冒泡排序中使用第二个for循环

标签 c arrays for-loop bubble-sort

<分区>

有人可以解释一下下面冒泡排序中第二个 for 循环的确切目的吗?我知道第一个循环正在查看数组的第“i”个整数,但第二个 for 循环到底在看什么?

请原谅我对这个话题的无知。我编码不到一周,对这个主题有些困惑。

void sort(int array[], int size) {
  for(int i = 0, x = size - 1; i < x; i++) {
    for(int j = 0; j < x - 1; j++) {
      if(array[j] > array[j + 1]) {
        int tmp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = tmp;
      }
    }
  }
}

最佳答案

考虑到您想实现冒泡排序,我猜您的第一个循环也是错误的,因为第一个循环告诉您对列表进行排序所需的遍数。在冒泡排序的情况下,它等于 Total Number of elements - 1 对 n 个元素的列表进行排序需要遍数 (n - 1),因此 i 的值 如果我没记错的话,我个人认为必须从 1 开始。此外,就您将变量声明为需要时而言,您提供的代码片段不像 C 语言编码风格。

第二个循环基本上是为了减少比较(元素数量 - 通过 - 1),在每次迭代之后,因为在每次通过时,我们将最大的元素放在右侧(逻辑上未排序的列表)。因此,由于该元素处于正确的位置,所以我们不必将其与其他元素进行比较。

  4 3 2 1 Original List
  3 2 1 4 Pass 1
        -
        Now since this above 4 is in it's rightful place
        we don't need to compare it with other elements.
        Hence we will start from the zeroth element and 
        compare two adjacent values, till 1 (for Pass 2)
        Here comparison will take place between 3 and 2,
        and since 3 is greater than 2, hence swapping 
        between 3 and 2, takes place. Now 3 is comapred
        with 1, again since greater value is on left
        side, so swapping will occur. Now 3 is not compared
        with 4, since both these values are in their 
        rightful place.
  2 1 3 4 Pass 2
      - 
        Now since this above 3 is in it's rightful place
        we don't need to compare it with other elements.
        Hence we will start from the zeroth element and 
        compare two adjacent values, till 1 (for Pass 3)
        Here only one comparison will occur, between
        2 and 1. After swapping 2 will come to it's rightful
        position. So no further comparison is needed.
  1 2 3 4 Pass 3
  Here the list is sorted, so no more comparisons, after Pass 3.    


void bubbleSort(int *ptr, int size)
{
        int pass = 1, i = 0, temp = 0;
        for (pass = 1; pass < size - 1; pass++)
        {
                for (i = 0; i <= size - pass - 1; i++)
                {
                        if (*(ptr + i) > *(ptr + i + 1))
                        {
                                temp = *(ptr + i);
                                *(ptr + i) = *(ptr + i + 1);
                                *(ptr + i + 1) = temp;
                        }
                }
                printf("Pass : %d\n", pass);
                for (temp = 0; temp < size; temp++)
                        printf("%d\t", *(ptr + temp));
                puts("");
        }
}

关于c - 在冒泡排序中使用第二个for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319520/

相关文章:

java - 如何找到数组中重复次数最多的数字?

arrays - 一个按钮的随机声音生成器,该按钮使用8个不同的.wav文件。每个文件都命名为sound1.wav,sound2.wav…sound8.wav

java - 用数组中包含的信息填充矩阵

java - 将 ArrayList 内容转换为带逗号的字符串

java - 将 4 个 for 循环变成 1 个

c - Windows下运行的C二进制文件中的已初始化数据段

c - c堆栈数据结构中的推送操作失败

c - 使用 OpenMP 并行递增数组元素

c++ - 在不知道长度的情况下找到数组的中间

javascript - 编写一个函数,返回其对应的所有键的所有值的数组