英文:
I implement this sorting program in c . Why this is not working?
问题
The sorting program you provided appears to have an issue in the inner loop where you are attempting to sort the array using the Bubble Sort algorithm. The issue is in these lines of code:
arr[i] = arr[j];
arr[j] = m;
You are swapping the elements incorrectly. It should be:
arr[j] = arr[i];
arr[i] = m;
Here's the corrected version of the code:
#include <stdio.h>
int main() {
int arr[5];
for (int i = 0; i < 5; i++)
scanf("%d", &arr[i]);
for (int i = 0; i < 5; i++) {
int m = arr[i];
for (int j = i + 1; j < 5; j++) {
if (m > arr[j]) {
// Swap the elements correctly
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
With this correction, the program should sort the array correctly using the Bubble Sort algorithm.
英文:
#include <stdio.h>
int main() {
int arr[5];
for(int i=0; i<5; i++)
scanf("%d",&arr[i]);
for(int i=0; i<5; i++)
{
int m=arr[i];
for(int j=i+1; j <5; j++)
{
if(m>arr[j])
{
arr[i]=arr[j];
arr[j]=m;
}
}
}
for(int i=0; i<5; i++)
printf("%d ",arr[i]);
return 0;
}
Why is this sorting program not working? Where is this program wrong? This program not sorting the array. Where is it wrong?
答案1
得分: 2
问题在于这个if语句
if(m>arr[j])
{
arr[i]=arr[j];
arr[j]=m;
}
它没有更新变量m
的值。
你需要写成这样:
if(m>arr[j])
{
arr[i]=arr[j];
arr[j]=m;
m = arr[i];
}
事实上,在内部for循环之前声明的变量m
是多余的。你可以在不使用变量m
的情况下写成:
if (arr[j] < arr[i])
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
如果只交换数组中的元素一次,循环可能会更有效。例如:
for (int i = 0; i < 5; i++)
{
int m = i;
for (int j = i + 1; j < 5; j++)
{
if (arr[j] < arr[m])
{
m = j;
}
}
if (m != i)
{
int tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
你可以写一个单独的函数。例如:
void selection_sort(int a[], size_t n)
{
for (size_t i = 0; i < n; i++)
{
size_t m = i;
for (size_t j = i + 1; j < n; j++)
{
if (a[j] < a[m])
{
m = j;
}
}
if (m != i)
{
int tmp = a[i];
a[i] = a[m];
a[m] = tmp;
}
}
}
然后像这样调用它:
selection_sort(arr, 5);
英文:
The problem is this if statement
if(m>arr[j])
{
arr[i]=arr[j];
arr[j]=m;
}
that does not update the value of the variable m
.
You need to write
if(m>arr[j])
{
arr[i]=arr[j];
arr[j]=m;
m = arr[i];
}
In fact the variable m
declared before the inner for loop is redundant. You could just write without using the variable m
if ( arr[j] < arr[i] )
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
The loops could be more efficient if to swap elements in the array only one time. For example
for ( int i = 0; i < 5; i++ )
{
int m = i;
for ( int j = i + 1; j < 5; j++ )
{
if ( arr[j] < arr[m] )
{
m = j;
}
}
if ( m != i )
{
int tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
You could write a separate function. For example
void selection_sort( int a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
size_t m = i;
for ( size_t j = i + 1; j < n; j++ )
{
if ( a[j] < a[m] )
{
m = j;
}
}
if ( m != i )
{
int tmp = a[i];
a[i] = a[m];
a[m] = tmp;
}
}
}
and call it like
selection_sort( arr, 5 );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论