| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 752 人关注过本帖
标题:[开源]明天考试,今天来几个程序
取消只看楼主 加入收藏
zhangzujin
Rank: 1
等 级:新手上路
帖 子:276
专家分:0
注 册:2005-5-9
收藏
 问题点数:0 回复次数:0 
[开源]明天考试,今天来几个程序

//SelectSort
#include<stdio.h>

void SelectSort(int test[],int n)
{
int i,j,small;
int tmp;

for(i=0;i<n-1;i++)
{
small=i;
for(j=i+1;j<n;j++)
if(test[j]<test[small])
small=j;
if(small!=i)
{
tmp=test[i];
test[i]=test[small];
test[small]=tmp;
}
}
}

int main( )
{
int n,test[100];
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
SelectSort(test,n);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");

return 0;
}


//QuickSort
#include<stdio.h>

void QuickSort(int test[],int low,int high)
{
int i,j;
int tmp;

i=low;
j=high;
tmp=test[low];

while(i<j)
{

while(i<j && tmp<=test[j])
j--;
if(i<j)
{
test[i]=test[j];
i++;
}

while(i<j && tmp>test[i])
i++;
if(i<j)
{
test[j]=test[i];
j--;
}
}

test[i]=tmp;

if(i-1>low)
QuickSort(test,low,i-1);
if(high>j+1)
QuickSort(test,j+1,high);
}

int main( )
{
int test[100],n;
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
QuickSort(test,0,n-1);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");
return 0;
}


//InsertSort
#include<stdio.h>

void InsertSort(int test[],int n)
{
int i,j;
int tmp;

for(i=0;i<n-1;i++)
{
tmp=test[i+1];
j=i;
while(j>=0 && test[j]>tmp)
{
test[j+1]=test[j];
j--;
}
test[j+1]=tmp;
}
}

int main( )
{
int test[100],n;
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
InsertSort(test,n);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");
return 0;
}

//BubbleSort
#include<stdio.h>

void BubbleSort(int test[],int n)
{
int i,j,flag=1;
int tmp;

for(i=1;i<n && flag==1;i++)
{
flag=0;
for(j=0;j<n-i;j++)
if(test[j]>test[j+1])
{
flag=1;
tmp=test[j];
test[j]=test[j+1];
test[j+1]=tmp;
}
}
}

int main( )
{
int test[100],n;
int i;

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&test[i]);
BubbleSort(test,n);
for(i=0;i<n;i++)
printf("%d ",test[i]);
printf("\n");
return 0;
}




搜索更多相关主题的帖子: int test 考试 开源 
2005-12-15 23:07
快速回复:[开源]明天考试,今天来几个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011199 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved