算法与数据结构之选择排序(C语言)
2024-10-11 12:24:16
#include<stdio.h>
#include<stdlib.h>
void SelectSort(int *a,int n);//预声明要调用的函数
int main(void)
{
int k;
int x[]={,,,,,,,,,};
for(k=;k<;k++)
printf("%d",x[k]);
printf("\n");
SelectSort(x,);
for( k=;k<;k++)
printf("%d",x[k]);
printf("\n");
system("pause"); return ;
} void SelectSort(int *a,int n)//数组元素是指针,数组数量
{
int i,j,t;//整型变量
int min;//最小值,索引号
for(i=;i<n-;i++)//n-1次扫描
{
min=i;//每次扫描前第一个元素(最小)
for(j=i+;j<n;j++)//第二轮扫描找到最小值并交换
{
if(a[j]<a[min])
min=j; }
t=a[i];//临时变量
a[i]=a[min];//
a[min]=t;
} }
选择排序基本思想:每一趟(如第n趟)在后面n-i+1(i=1,2,…,n-1)个待排序元素中选取关键字最小的元素,作为有序子序列的第i个元素,直到第n-1趟做完,待排序元素只剩下一个,就不用再选了。
最新文章
- 弹出popwindow 背景变暗
- android opengl es代码功能
- iOS闪烁效果工具 GlitchLabel
- jQuery动画效果实现
- 第六篇、CSS属性
- paip.gch预编译头不生效的原因以及解决:
- codeforces 622E. Ants in Leaves
- ie6下margin双倍距的问题
- JMeter之Throughput Controller详解(实验)
- 最常见的三个排序(冒泡、直接插入、快速)的JS实现
- Sql Server的艺术(六) SQL 子查询,创建使用返回多行的子查询,子查询创建视图
- 题目1032:ZOJ
- 从0开始的Python学习016异常
- 「技巧」如何快速安装 Sketch 插件
- 使用Autoencoder进行降维
- BZOJ.2655.calc(DP/容斥 拉格朗日插值)
- 华为5G在印度被禁
- WPF 程序在 Windows XP 下报错:The image format is unrecognized.
- HDU 4632 Palindrome subsequence(区间DP求回文子序列数)
- UVA 10194 (13.08.05)
热门文章
- uva10001 Garden of Eden
- Xcode 编译的哪些错误
- Java 中包装类wrapped type之间以及和primitive type的比较
- Maven 常用命令, 备忘
- Centos5, 6下更改系统时间和时区
- JS给文本框赋值后,在页面后台取不到文本框值的解决方法
- python执行linux shell管道输出内容
- win7 IIS7.5配置伪静态
- JS明确指定函数的接受者
- 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child...";异常处理