c语言选择排序
来源:互联网 发布:傅园慧网络直播 编辑:程序博客网 时间:2024/05/01 23:18
#include <stdio.h>
#define MAX 255
int R[MAX];
void Select_Sort(int n)
{
int i,j,k;
for(i=1;i<n;i++)
{/* 做第i趟排序(1≤i≤n-1) */
k=i;
for(j=i+1;j<=n;j++) /* 在当前无序区R[i..n]中选key最小的记录R[k] */
if(R[j]<R[k])
k=j; /* k记下目前找到的最小关键字所在的位置 */
if(k!=i)
{ /* 交换R[i]和R[k] */
R[0]=R[i]; R[i]=R[k]; R[k]=R[0]; /* R[0]作暂存单元 */
} /* endif */
} /* endfor */
} /* end of Select_Sort */
void main()
{
int i,n;
system("cls");
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i++)
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
Select_Sort(n);
puts("\nThe sequence after select_sort is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getchar();
getchar();
}
#define MAX 255
int R[MAX];
void Select_Sort(int n)
{
int i,j,k;
for(i=1;i<n;i++)
{/* 做第i趟排序(1≤i≤n-1) */
k=i;
for(j=i+1;j<=n;j++) /* 在当前无序区R[i..n]中选key最小的记录R[k] */
if(R[j]<R[k])
k=j; /* k记下目前找到的最小关键字所在的位置 */
if(k!=i)
{ /* 交换R[i]和R[k] */
R[0]=R[i]; R[i]=R[k]; R[k]=R[0]; /* R[0]作暂存单元 */
} /* endif */
} /* endfor */
} /* end of Select_Sort */
void main()
{
int i,n;
system("cls");
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i++)
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
Select_Sort(n);
puts("\nThe sequence after select_sort is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getchar();
getchar();
}
0 0
- C语言选择排序
- c语言选择排序
- c语言选择排序
- C语言---选择排序
- C语言 选择排序
- c语言选择排序
- 【C语言】选择排序
- C语言:选择排序
- C语言 选择排序
- C语言选择排序
- c语言选择排序
- C语言------选择排序
- 选择排序(C语言)
- C语言选择排序
- 选择排序 --C语言数据结构
- 数据结构--选择排序(C语言)
- C语言学习-选择排序
- 选择排序C语言实现
- 制作各种docker镜像
- jsp 页面常见的中文乱码问题总结
- 模板题-----DFS深度搜索 HDU2952
- 2013 results(29 Aug,2013)
- core java volume
- c语言选择排序
- 谈.Net委托与线程——解决窗体假死(三)
- HDU 4734 F(x)(数位DP)
- docker源码阅读
- hdoj-1233-还是畅通工程
- 英语笔试题1
- malloc内存分配
- swift 笔记
- JQuery中$.ajax()方法参数详解