簡體   English   中英

C中的冒泡排序

[英]Bubble Sort in C

我正在嘗試在 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 的數字,這可能會導致索引越界異常。

#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(j=0;j<n-i-1;j++){
}
/*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;
 }
/*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]);
}
   #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;
      }

我試圖涵蓋所有可能的條件,以減少冒泡排序的通過和比較,以減少總時間。 這是我的代碼...

#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();
}

結果:-

冒泡排序實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM