飞翔的荷兰人灬 2015-05-17 05:55 采纳率: 100%
浏览 1854
已采纳

noj一道冒泡排序,不能通过,求大神解答

题目描述
给定输入排序元素数目n和相应的n个元素,写出程序,利用内排序算法中冒泡排序算法进行排序,并输出排序过程中每趟及最后结果的相应序列。

输入

共两行,第一行给出排序元素数目n,第二行给出n个元素,1≤n≤400,每个元素值范围为 [0,100000)

输出

三个部分
第1部分为两行,第1行输出文字“Source:”,第2行给出原始序列;
第2部分,开始输出文字“Bubble Sort:”,后续输出简单选择排序过程;
第3部分,开始输出文字“Result”,后续输出排序结果。
样例输入

 7
48 36 68 72 12 48 2

样例输出

 Source:
(48 36 68 72 12 48 2)
Bubble Sort:
(36 48 68 12 48 2) 72
(36 48 12 48 2) 68 72
(36 12 48 2) 48 68 72
(12 36 2) 48 48 68 72
(12 2) 36 48 48 68 72
(2) 12 36 48 48 68 72
Result
(2 12 36 48 48 68 72)

 #include<stdio.h>
#define N 400

void init(int a[],int n)
{   int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

}
void bubberSort(int a[],int n)
{
    int i,j,t,k=1;
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j+1];
                a[j+1]=a[j];
                a[j]=t;
            }
        }
        putchar('(');
        for(j=0;j<n;j++)
        {
            if(j==0)
            {
              if(i!=n-1-1)
              printf("%d",a[j]);
              else printf("%d)",a[j]);   
            }
            else if(j==n-1-k)
            {
                printf("%c%d",32,a[j]);
                putchar(')');
            }
            else printf("%c%d",32,a[j]);

        }
          k++;
            putchar('\n');
    }

 }
void myPrint(int a[],int n)
{
    int i;
    putchar('(');
    for(i=0;i<n;i++)
    {
         if(i==n-1)
        {
            printf("%d",a[i]);
            putchar(')');
        }
        else printf("%d%c",a[i],32);

    }
    printf("\n");
}  
int main()
{
    int a[N],n;
    scanf("%d",&n);
    init(a,n);
    puts("Source: ");
    myPrint(a,n);
    puts("BubbleSort: ");
    bubberSort(a,n);
    puts("Result: ");
    myPrint(a,n);
    return 0;     
    getchar(); 
 }



显示答案错误
我的代码怎么修改才能通过啊
原题链接http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1063
  • 写回答

2条回答 默认 最新

  • nonprocess 2015-05-17 06:24
    关注

    题目说是Bubble Sort,但是在输出的第二部分说用简单的选择排序。。

    我按选择排序写了一个

    var
    a:array[1..1000] of longint;
    i,j,n,k,l:longint;
    begin
    readln(n);
    for i:=1 to n do read(a[i]);
    writeln('Source:');
    for i:=1 to n do write(a[i],' ');
    writeln;
    writeln('Bubble Sort:');
    for i:=1 to n-1 do
    for j:=i+1 to n do
    if a[i]>a[j] then
    begin
    k:=a[i];a[i]:=a[j];a[j]:=k;
    for l:=1 to n do write(a[l],' ');
    writeln;
    end;
    writeln('Result');
    for i:=1 to n do write(a[i],' ');
    end.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?