数据结构基础 算法实现
2015-09-05 11:59
519 查看
本程序就是把数据结构基础上的代码运行起来,用转载比较合适。
#define MAX_SIZE 128
void sort(int a[], int n)
{
for(int i=0;i<n-1;i++)
{
int min = i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
min = j;
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main()
{
//当前未排序的整数中,找出最小一个,把它放在当前有序表的后一个位置
int n;
int a[MAX_SIZE];
printf("Enter the number of numbers to generate(the max size is %d): ",MAX_SIZE);
scanf("%d",&n);//引号内不能有空格
if(n<1 || n>MAX_SIZE)
{
fprintf(stderr,"Improper value of n\n");
exit(EXIT_FAILURE);
}
for(int i =0;i<n;i++)
{
a[i] = rand()%1000;
printf("%d ", a[i]);
}
sort(a,n);
printf("\n Sorted array: \n");
for(i =0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
return -1;
}
运行不过的话加头文件。
#define MAX_SIZE 128
void sort(int a[], int n)
{
for(int i=0;i<n-1;i++)
{
int min = i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
min = j;
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main()
{
//当前未排序的整数中,找出最小一个,把它放在当前有序表的后一个位置
int n;
int a[MAX_SIZE];
printf("Enter the number of numbers to generate(the max size is %d): ",MAX_SIZE);
scanf("%d",&n);//引号内不能有空格
if(n<1 || n>MAX_SIZE)
{
fprintf(stderr,"Improper value of n\n");
exit(EXIT_FAILURE);
}
for(int i =0;i<n;i++)
{
a[i] = rand()%1000;
printf("%d ", a[i]);
}
sort(a,n);
printf("\n Sorted array: \n");
for(i =0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
return -1;
}
运行不过的话加头文件。
相关文章推荐
- 数据结构 - 内存
- 数据结构基础题(第一篇)
- 数据结构 - 指针,数组,结构体
- 数据结构之反黄牛火车票订票系统
- 数据结构在软件编程中的地位
- 单链表的基本操作-数据结构
- PTA 数据结构与算法题目集 5-1 最大子列和问题
- 2014找工作总结-机会往往留给有准备的人
- 数据结构学习之直接插入排序
- 数据结构--图的相关操作实现
- 数据结构学习之选择排序
- 数据结构之单链表 C++实现
- 中国大学MOOC-陈越、何钦铭-数据结构 Maximum Subsequence Sum
- 【数据结构】图
- 数据结构必须掌握 栈---压栈和出栈
- Android开发中高效的数据结构用SparseArray代替HashMap
- 2015.09.04 数据结构笔记
- 堆数据结构
- 数据结构---数组(5)
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结