【问题标题】:Bubble Sort in CC中的冒泡排序
【发布时间】:2014-02-25 19:39:11
【问题描述】:

我正在尝试在 C 中实现冒泡排序,并且已经走到了这一步,但它的排序也不正确。

#include<stdio.h>

int main()
{
    int n, i, j, a[5], b, temp;
    printf("Enter the number of elements to be sorted\n");
    scanf("%d", &n);
    for(i = 0; i < n; ++i)
    {
        printf("%d - Enter the elements - ", i);
        scanf("%d", &a[i]);
    }
    for(i = 0; i < n; ++i)
    {
        for(j = 0; j < n+1; ++j)
        {
            if(a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }
    for (i = 0; i < n; ++i)
    {
        printf("%d\n", a[i]);
    }
    return 0;
}

输入

2
12
1
13

输出

2
1
12
13

我错过了什么?

【问题讨论】:

  • 仅供参考,您的程序有一个重大缺陷。您询问用户他们将输入多少个元素,但您在此之前初始化了您的数组。如果有人输入大于 5 的数字,这可能会导致索引越界异常。干杯。
  • 我已经用解释更新了我的答案。我希望你也解决我在之前的评论中提到的问题。干杯。

标签: c sorting


【解决方案1】:

所以现在剧情已经过去了,你的代码的问题是你没有在你的内部循环中使用正确的索引。此外,您的内部循环计数器的条件检查不正确。此外,正如我在对您的问题的评论中提到的那样,您的代码中有一个缺陷(我尚未修复),您在询问用户他们想要输入多少元素之前初始化您的数组。如果用户输入大于 5 的数字,这可能会导致索引越界异常。

#include<stdio.h>

int main()
{
    int n, i, j, a[5], b, temp;
    printf("Enter the number of elements to be sorted\n");
    scanf("%d", &n);
    for(i = 0; i < n; ++i)
    {
        printf("%d - Enter the elements - ", i);
        scanf("%d", &a[i]);
    }
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n-1; j++)
        {
            if(a[j] > a[j+1])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
    for (i = 0; i < n; ++i)
    {
        printf("%d\n", a[i]);
    }
    return 0;
}

【讨论】:

  • 实际上前缀与后缀在 for 循环中没有区别。 for 循环如何执行遵循 4 个不同的步骤。首先:在 for 循环中执行三个语句中的第一个。第二:检查for循环中的第二条语句是否为真,它会继续执行第三:运行for循环的主体。第四:执行for循环中的第三条语句。通常这类似于“++i”或“i++”
  • @Cairnarvon 你为什么认为,它不值得投票?
  • @rcomp 当我写这个并且它被赞成时,他的回答是循环的第三部分应该使用后增量而不是前增量,这太荒谬了。 (就目前而言,它仍然是一个非常糟糕的答案,因为它只是一个代码转储而不是解释,但至少在技术上它是有效的。)
  • @rcomp,最初当 Sly Raskal 上传它时,它在功能上等同于您问题中提供的来源。他也是一个狡猾的拉斯卡尔,随着您问题的每一个新答案的出现,他都会编辑他的答案,使他的答案最终正确。
  • @Cairnarvon,我在 iPad 上打字,至少可以这么说。我一边编辑一边编辑我的帖子,但希望在其他人发布代码之前尽快将代码放入其中。我正计划用文本信息更新帖子以帮助 OP。干杯。
【解决方案2】:

您的第二个循环不正确。

for(j=0;j<n-i-1;j++){
}

【讨论】:

    【解决方案3】:
    /*By your approach , in inner loop you are not checking the each elements . So change i to j in swapping , and limit of j should be till n-1*/
    
    
      #include<stdio.h>
    
    int main()
    {
    int n, i, j, a[10], b, temp=0;
    printf("Enter the number of elements to be sorted\n");
    scanf("%d", &n);
    for(i = 0; i < n; ++i)
    {
        printf("%d - Enter the elements - ", i);
        scanf("%d", &a[i]);
    }
    for(i = 0; i < n; ++i)
    {
        for(j = 0; j < n-1; ++j)     // notice limit , also complexity can be reduced by changing        to(j<n-i-1)
        {
            if(a[j] > a[j+1])
            {
                temp = a[j];        // changed variable 
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
    for (i = 0; i < n; ++i)
    {
        printf("%d\n", a[i]);
    }
    return 0;
     }
    

    【讨论】:

      【解决方案4】:
      /*You May check this code it will help you*/
      #include<stdio.h>
      void bubble_sort(int a[],int n);
      void bubble_sort(int a[],int n)
      {
          int i,j;
          int temp;
      
          for(i=0;i<n;i++)
          {
              for (j=0; j<=n-i-1;j++)
              {
                  if (a[j]>a[j+1])
                  {
                      temp = a[j];
                      a[j] = a[j+1];
                      a[j+1] = temp;
                  }
              }
      
          }
      }
      main()
      {
          int a[10];
          int i,n;
          printf("Enter the number\n");
          scanf("%d",&n);
          printf("Enter the number\n");
          for(i=0;i<n;i++)
          scanf("%d",&a[i]);
          bubble_sort(a,n);
          printf("sorted elements are\n");
          for(i=0;i<n;i++)
          printf("%d\n",a[i]);
      }
      

      【讨论】:

        【解决方案5】:
           #include<stdio.h>
        
            int main()
            {
                   int n, i, j, a[5], b, temp;
                    printf("Enter the number of elements to be sorted\n");
                   scanf("%d", &n);
                   for(i = 0; i < n; ++i)
                   {
                         printf("%d - Enter the elements - ", i);
                         scanf("%d", &a[i]);
                     }
                  for(i = 0; i < n; ++i)
                   {
                            for(j = 0; j < n; ++j)
                             {
                                  if(a[j] > a[j+1]) //change the varible instead of i to j
                                  {
                                         temp = a[j];
                                         a[j] = a[j+1];
                                         a[j+1] = temp;
                                    }
                           }
                    }
                    for (i = 0; i < n; ++i)
                    {
                          printf("%d\n", a[i]);
                     }
                     return 0;
              }
        

        【讨论】:

          【解决方案6】:

          我已尝试涵盖所有可能的条件,以减少冒泡排序的通过和比较,从而减少总时间。这是我的代码...

          #include <stdio.h>
          #include <conio.h>
          
          void bubbleSort(int n){
              int arr[n],i,j,temp=0;
              int swapFlag = 0;
          
              printf("\nInsert %d elements:\n",n);
              for(i=0;i<n;i++){
                  scanf("%d",&arr[i]);
              }
              printf("Insert complete.\n\n");
          
              printf("Your array looks like:\n");
              for(i=0;i<n;i++){
                  printf("%d ",arr[i]);
              }
          
              //Bubble Sort Algorithm
              for(i=0;i<n-1;i++){
                  swapFlag = 0;
                  for(j=0;j<n-i-1;j++){
                      if(arr[j]>arr[j+1]){
                          swapFlag = 1;
                          //Swapping unordered pairs
                          temp = arr[j];
                          arr[j] = arr[j+1];
                          arr[j+1] = temp;
                      }
                  }
                  //Condition to reduce number of passes & comparisons
                  if(swapFlag == 0){
                      break;
                  }
              }
          
              printf("\n\nAfter sorting the array looks like:\n");
              for(i=0;i<n;i++){
                  printf("%d ",arr[i]);
              }
          }
          
          void main(){
              int n;
              printf("Enter number of array elements: ");
              scanf("%d",&n);
              bubbleSort(n);
              getch();
          }
          

          结果:-

          【讨论】:

            猜你喜欢
            • 2014-03-26
            • 2018-11-13
            • 2017-10-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-08-11
            相关资源
            最近更新 更多