Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343931
  • 博文数量: 213
  • 博客积分: 566
  • 博客等级: 中士
  • 技术积分: 1210
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 13:09
文章分类

全部博文(213)

文章存档

2015年(1)

2013年(7)

2012年(128)

2011年(77)

分类:

2011-03-25 17:04:22

原文地址:C程序练习-冒泡排序 作者:chengxiaopeng

代码如下:

#include <stdio.h>

void maopao_asc_sort(int[],int);
void print_shuzu(int[],int);
void maopao_desc_sort(int[],int);
int main()
{
    int a[11]={30,5,20,4,50,60,84,58,31,26,33};
    printf("the soure data is :\n");
    print_shuzu(a,11);
    maopao_asc_sort(a,11);
    printf("\nthe sorted numbers:\n");
    print_shuzu(a,11);
    printf("\nthe desc sorted numbers:\n");
    maopao_desc_sort(a,11);
    print_shuzu(a,11);
    system("pause");
    return 1;
}

void maopao_asc_sort(int a[],int n)
{
    int i,j,t;
    for (i=0;i < n - 1;i++)
    {
        for (j=0;j<n - 1 - i;j++)
        {
            if(a[j] > a[j+1])
            {
                    t = a[j];
                    a[j] = a[j+1];
                    a[j+1] = t;
            }
        }
    }
}

void maopao_desc_sort(int a[],int n)
{
    int i,j,t;
    for (i=0;i < n - 1;i++)
    {
        for (j=0;j<n - 1 - i;j++)
        {
            if(a[j] < a[j+1])
            {
                    t = a[j];
                    a[j] = a[j+1];
                    a[j+1] = t;
            }
        }
    }
}

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


阅读(480) | 评论(0) | 转发(0) |
0

上一篇:C程序练习-查找数

下一篇:筛法求素数

给主人留下些什么吧!~~