鱼C论坛

 找回密码
 立即注册
查看: 1598|回复: 1

[已解决]萌新求助:想让大家看看问题出在哪里

[复制链接]
发表于 2022-10-15 17:05:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
冒泡排序(Bubble Sort),也称为沉降排序(Sinking Sort),之所以称其为冒泡排序,是因为算法中值相对较小的数据会像水中的气泡一样逐渐上升到数组的最顶端。与此同时,较大的数据逐渐地下沉到数组的底部。这个处理过程需在整个数组范围内反复执行多遍。每一遍执行时,比较相邻的两个元素。若顺序不对,则将其位置交换,当没有数据需要交换时,数据也就排好序了。编程将排序函数DataSort()改用冒泡法实现。
**输入格式要求:"%d"  提示信息:"Input n:"  "Input %d numbers:"
**输出格式要求:"Sorting results:"  "%4d"
程序运行示例如下:
Input n:10
Input 10 numbers: 2 9 3 4 0 6 8 7 5 1
Sorting results:   0   1   2   3   4   5   6   7   8   9

这是我的错误答案
#include<stdio.h>
#define N 40
void DataSort(int a[],int n);
int main()
{
    int n , i , a[N];
    printf("Input n:");
    scanf(" %d",&n);
    printf("Input %d numbers:",n);
    for(  i = 0  ;  i < n  ;  i++ )
    {
        scanf(" %d",&a[i]);
    }
    printf("Sorting results:" );
     DataSort(a, n);
    return  0 ;
}
void DataSort(int a[],int n)//排序数组
{
  int i , j , k ,temp;
    for(  i  = 0  ; i < n - 1  ; i++ ){
        for( j = i + 1  ; j < n ;  j++  ){
            if( a[j] < a[i]  ){
                temp = a[j];
                a[j]  =  a[i];
                a[i]  =  temp;
            }
        }
        }
        for (i=0;i<n;i++)
    {
        printf("%4d",a[i]);
    }
}


标准答案:
#include <stdio.h>
#include <stdlib.h>
#define N 40

void DataSort(int a[],int n);

void DataSort(int a[],int n)
{
    int i,j,temp;
    for (i=0;i<n-1;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("%4d",a[i]);
    }
}

int main()
{
    int a[N],n,i;
    printf("Input n:");
    scanf("%d",&n);
    printf("Input %d numbers:",n);
    for (i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Sorting results:");
    DataSort(a,n);
    return 0;
}

最佳答案
2022-10-15 17:23:51
本帖最后由 jackz007 于 2022-10-15 17:29 编辑
#include<stdio.h>

#define N 40

void DataSort(int a[] , int n) //排序数组
{
        int i , j , k ,temp;
        for(i = 0  ; i < n - 1 ; i ++) {
                for(j = i + 1 ; j < n ;  j ++) {
                        if(a[j] < a[i]) {
                                temp = a[j]                ;
                                a[j]  =  a[i]              ;
                                a[i]  =  temp              ;
                        }
                }
        }
}

int main(void)
{
        int n , i , a[N]                                   ;
        printf("Input n : ")                               ;
        scanf("%d" , & n)                                  ;
        printf("Input %d numbers : " , n)                  ;
        for(i = 0  ;  i < n  ;  i ++) scanf("%d" , & a[i]) ;
        DataSort(a , n)                                    ;
        printf("Sorting results:" )                        ;
        DataSort(a, n)                                     ;
        for(i = 0 ; i < n ; i ++) printf("%4d" , a[i])     ;
        printf("\n")                                       ;
}
        你的代码基本没有问题,简单整理了一下:
        编译、运行实况:
D:\[00.Exerciese.2022]\C>g++ -o b b.c

D:\[00.Exerciese.2022]\C>b
Input n : 10
Input 10 numbers : 2 9 3 4 0 6 8 7 5 1
Sorting results :    0   1   2   3   4   5   6   7   8   9

D:\[00.Exerciese.2022]\C>
      修改的地方主要是把数据显示从 DataSort() 挪到了 main() 中,其次,就是去掉了 scanf() 格式描述符中的一个前导空格。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-15 17:23:51 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-10-15 17:29 编辑
#include<stdio.h>

#define N 40

void DataSort(int a[] , int n) //排序数组
{
        int i , j , k ,temp;
        for(i = 0  ; i < n - 1 ; i ++) {
                for(j = i + 1 ; j < n ;  j ++) {
                        if(a[j] < a[i]) {
                                temp = a[j]                ;
                                a[j]  =  a[i]              ;
                                a[i]  =  temp              ;
                        }
                }
        }
}

int main(void)
{
        int n , i , a[N]                                   ;
        printf("Input n : ")                               ;
        scanf("%d" , & n)                                  ;
        printf("Input %d numbers : " , n)                  ;
        for(i = 0  ;  i < n  ;  i ++) scanf("%d" , & a[i]) ;
        DataSort(a , n)                                    ;
        printf("Sorting results:" )                        ;
        DataSort(a, n)                                     ;
        for(i = 0 ; i < n ; i ++) printf("%4d" , a[i])     ;
        printf("\n")                                       ;
}
        你的代码基本没有问题,简单整理了一下:
        编译、运行实况:
D:\[00.Exerciese.2022]\C>g++ -o b b.c

D:\[00.Exerciese.2022]\C>b
Input n : 10
Input 10 numbers : 2 9 3 4 0 6 8 7 5 1
Sorting results :    0   1   2   3   4   5   6   7   8   9

D:\[00.Exerciese.2022]\C>
      修改的地方主要是把数据显示从 DataSort() 挪到了 main() 中,其次,就是去掉了 scanf() 格式描述符中的一个前导空格。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 18:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表