【问题标题】:bubble sort arrays in cc中的冒泡排序数组
【发布时间】:2015-01-27 14:06:38
【问题描述】:

我已经研究过这个话题,尤其是Bubble Sort in C。 我正在研究带有数组的冒泡排序算法,如果我绘制它,我会理解这个概念,但是在实现循环时我迷失了;根据我的理解,我知道一个循环是传球次数,另一个循环是交换。我对所有变量和循环感到困惑......有人可以为我澄清循环部分吗?如果有人知道这个主题的任何精通视频tuts,请将它们发送给我!我试着把每一步分解成块,这样我可以更好地遵循它,但是 我将在我的代码中注释我不理解的内容。感谢一如既往的帮助

    #include <stdio.h>
    int main (void)
    {
    int a[20], i, j, temp;
    int SIZE= 20;<==== A[20] SPECIFIES 20 ELEMENTS IN MY ARRAY and I HAVE INT SIZE = 20, can I just pick one or the other for use as the variable?

  //ask user to enter 20 numbers for the array's elements
   printf("Enter the elements be sorted");
   scanf("%d", &SIZE);
   for(i = 0; i<SIZE; i++)
    scanf("%d", &a[i]);

   //bubble sorting<========I'M COMPLETELY LOST HERE
   for(SIZE-2;i<20; i--){
    for(j=0;j<=i;j++){
      if(a[j]>a[j + 1]{
         temp = a[j]
            a[j] = a [j+1];
            a[j+1]= temp;
    }
    }
    }
    printf("After sorting: ");
    for(i=0; i<SIZE; i++)
    printf(" %d", a[i]);
    }
    }

【问题讨论】:

  • for(SIZE-2;i&lt;20; i--){ 错误。
  • 好的,我刚改成i++。你能解释为什么它不是我——吗?我仍然在循环概念方面遇到问题,所以我很挣扎,哈哈
  • 谢谢,这很有帮助!

标签: c arrays sorting


【解决方案1】:

http://www.youtube.com/watch?v=lyZQPjUT5B4 这用有趣的方式解释!

【讨论】:

  • 谢谢你,我是视觉学习者,再次感谢我只是想把这个概念理解下来,我很感激你没有咬我的头
猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2013-09-28
  • 2020-09-13
  • 2015-04-10
  • 2014-03-26
  • 2018-11-13
相关资源
最近更新 更多