# 在气泡分选中得到错误的输出Getting Wrong output in bubble sorting

``````#include <stdio.h>
#define SIZE 5
void bubblesort(int A[]);
int main(void)
{
int A[SIZE]={0};
int i;
puts("Enter value to store it in integer");
for(i=0;i<SIZE;i++)
{
scanf("%d",&A[i]);
}
puts("");
bubblesort(A);
}

void bubblesort(int A[])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<4;j++)
{
if(A[j]>A[j+1])
{
int temp;
temp=A[j+1];
A[j]=A[j+1];
A[j]=temp;
}
}
}
for(i=0;i<SIZE;i++)
{
printf("%d ",A[i]);
}
}
``````

## 3 个回复3

### ===============>>#1 票数：1

``````int temp;
temp=A[j+1];
A[j]=A[j+1];
A[j]=temp;
``````

### ===============>>#2 票数：1 已采纳

``````         temp=A[j+1];  // stores A[j+1] in temp
A[j]=A[j+1];  // stores A[j+1] in A[j] - the value in A[j] is lost
A[j]=temp;    // stores temp in A[j]
``````

``````         temp=A[j+1];   // stores A[j+1] in temp
A[j+1]=A[j];   // stores A[j] in A[j+1]
A[j]=temp;     // stores temp in A[j]
``````

``````for(j=0;j<4;j++)
``````

``````for(j=0;j<i;j++)
``````

### ===============>>#3 票数：1

``````   #include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter Total Terms\n");
scanf("%d", &n);

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

for (c = 0; c < n; c++)
scanf("%d", &array[c]);
//bubble sort logic
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /*  use < For decreasing order */
{
swap       = array[d];
array[d]   = array[d+1];
array[d+1] = swap;
}
}
}

printf("ascending order:\n");

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

return 0;
}
``````

1回复

2回复

1回复

1回复

1回复

4回复

1回复

1回复

1回复

1回复