当前位置: 首页 >> 程序设计 >> 数据结构和算法 >> 冒泡和选择排序实现
 

冒泡和选择排序实现

作者:normalnotebook      来源:     发表时间:2006-06-20     浏览次数:      字号:    

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//冒泡排序
void bubbleSort(int *a,int len)
{
     int i,j,temp;
     for(i = 0;i<len-1;i++)
     {
          for(j=0;j<len -i-1;j++)
          {
               if(a[j]>a[j+1])
               {
                   temp=a[j];
                   a[j]=a[j+1];
                   a[j+1]=temp;
               }
         }
    }
}
//选择排序
void selectSort(int *a,int len)
{
     int i,j,temp,result;
     for(i=0;i<len-1 ;i++)
     {
          temp=i;
          for(j=i+1;j<len-1;j++)
          {
                if(a[j]<a[temp])
                {
                       temp=j;
                }
         }
         if(temp!=i)
          {
               result=a[i];
               a[i]=a[temp];
               a[i]=result;
          } 
     }
}

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


int main()
{
    int value[10]={38,6,14,9,7,33,67,12,34,51};
    printf("bubbleSort result:\n");
    bubbleSort(value,10);
    print(value,10);
    printf("bubbleSort result:\n");
    selectSort(value,10);
    print(value,10);
    return 0;

}

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
要求内存量最大的排序是哪种啊 ??有谁知道 请速答谢谢
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •