码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡,选择排序

时间:2015-10-14 12:25:53      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 101

//swap function bug
//void swap(int x,int y) swap change the location of the two number
void swap(int &x,int &y)//why need &
{
int t;
t=x;
x=y;
y=t;
}


void sort(int list[],int n)
{
int i,j,min;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n-1;j++)
{
if(list[j]<list[min])
{
min=j;
}
}
//please think about it
//what‘s better ?
swap(list[i],list[min]);
}
}//the main ideal is looking for the smallest number


int main(void)
{
int i,n;
int list[MAX_SIZE];//定义数组
printf("enter the number to generate:");
scanf("%d",&n);

/*The input sign n must between 1 and 100 */

if(n<1 || n>MAX_SIZE)
{
/*int fprintf(FILE *stream,char *format,[argument]) */
fprintf(stderr,"improper value of n");
exit(EXIT_FAILURE);
}

//input number
printf("input number:\n");
for (i=0;i<n;i++)
{
/*randomlys generate number*/
list[i]=rand()%1000;
//it‘s good ?
printf("%d ",list[i]);
}
printf("\ninput number end\n");

//sort function
printf("sorting...\n");
sort(list,n);
printf("sort ok\n");

/*print out sorted numbers*/
printf("output number\n");
for(i=0;i<n;i++)
{
printf("%d ",list[i]);
}
printf("\noutput end\n");

exit(0);
}

冒泡,选择排序

标签:

原文地址:http://www.cnblogs.com/ainixiaohai/p/4876604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!