没有合适的资源?快使用搜索试试~ 我知道了~
C++常用排序算法
需积分: 10 1 下载量 134 浏览量
2014-06-03
11:45:18
上传
评论
收藏 3KB TXT 举报
温馨提示
试读
5页
C++常用排序算法
资源推荐
资源详情
资源评论
C++常用排序算法
//选择排序法SelectionSort(int arr[],int n)
template <typename T>
void SelectionSort(T arr[],int n)
{
int smallIndex; //表中最小元素的下标
int pass,j; //用来扫描子表的下标
T temp; //用来交换表元素的临时变量
//pass的范围是0~n-2
for (pass=0;pass<n-1;pass++)
{
//从下标pass开始扫描子表
smallIndex=pass;
//j遍历整个子表arr[pass+1]到arr[n-1]
for(j=pass+1;j<n;j++)
//如果找到更小的元素,则将该位置赋值给smallIndex
if(arr[j]<arr[smallIndex])
smallIndex=j;
//如果smallIndex和pass不在相同的位置
//则将子表中的最小项与arr[pass]交换
if(smallIndex!=pass)
{
temp=arr[pass];
arr[pass]=arr[smallIndex];
arr[smallIndex]=temp;
//选择排序法SelectionSort(int arr[],int n)
template <typename T>
void SelectionSort(T arr[],int n)
{
int smallIndex; //表中最小元素的下标
int pass,j; //用来扫描子表的下标
T temp; //用来交换表元素的临时变量
//pass的范围是0~n-2
for (pass=0;pass<n-1;pass++)
{
//从下标pass开始扫描子表
smallIndex=pass;
//j遍历整个子表arr[pass+1]到arr[n-1]
for(j=pass+1;j<n;j++)
//如果找到更小的元素,则将该位置赋值给smallIndex
if(arr[j]<arr[smallIndex])
smallIndex=j;
//如果smallIndex和pass不在相同的位置
//则将子表中的最小项与arr[pass]交换
if(smallIndex!=pass)
{
temp=arr[pass];
arr[pass]=arr[smallIndex];
arr[smallIndex]=temp;
资源评论
木棉花儿冷
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功