简单选择排序(Simple Selection Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
|
#include<iostream>
using namespace std;
//简单选择排序
int simpleSelectionSort(int* arr,int length);
void swap(int& elem1,int& elem2);
void test();
void printArr(int* arr,int length);
void swap(int& elem1,int& elem2)
{
int tmp = elem1;
elem1 = elem2;
elem2 = tmp;
}
int simpleSelectionSort(int* arr,int length)
{
if(NULL==arr||length<=0)
return -1;
int minPos = 0;
for(int idx=0;idx!=length;++idx)
{
minPos = idx;
for(int iidx=idx+1;iidx<length;++iidx)
{
if(arr[iidx]<arr[minPos])
{
minPos = iidx;
}
}
if(idx!=minPos)
{
swap(arr[idx],arr[minPos]);
}
}
return 0;
}
|
void printArr(int* arr,int length)
{
if(NULL==arr||length<=0)
return ;
for(int idx=0;idx!=length;++idx)
{
cout<<arr[idx]<<" ";
}
cout<<endl;
}
void test()
{
int arr[] = {6,5,3,1,8,7,2,4};
printArr(arr,8);
simpleSelectionSort(arr,8);
printArr(arr,8);
cout<<endl;
int arr1[] = {1,2,3,4,5,6,7,8};
printArr(arr1,8);
simpleSelectionSort(arr1,8);
printArr(arr1,8);
cout<<endl;
int arr2[] = {2,2,2,2};
printArr(arr2,4);
simpleSelectionSort(arr2,4);
printArr(arr2,4);
cout<<endl;
int arr3[] = {2,2,1,2};
printArr(arr3,4);
simpleSelectionSort(arr3,4);
printArr(arr3,4);
cout<<endl;
int* arr4 = NULL;
printArr(arr4,4);
simpleSelectionSort(arr4,4);
printArr(arr4,4);
cout<<endl;
}
int main()
{
test();
system("pause");
}
|
简单选择排序(Simple Selection Sort)的更多相关文章
- 数据结构 - 只需选择排序(simple selection sort) 详细说明 和 代码(C++)
数据结构 - 只需选择排序(simple selection sort) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/28601 ...
- 选择排序(1)——简单选择排序(selection sort)
选择排序是一种很常见的排序算法,它需要对数组 中的元素进行多次遍历.每经过一次循环,选择最小的元素并把它放在靠近数组前端的位置. 代码实现: public static void selectionS ...
- 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现
选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...
- 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...
- js 实现排序算法 -- 选择排序(Selection Sort)
原文: 十大经典排序算法(动图演示) 选择排序(Selection Sort) 选择排序(Selection-sort)是一种简单直观的排序算法.它的工作原理:首先在未排序序列中找到最小(大)元素,存 ...
- 【排序基础】1、选择排序法 - Selection Sort
文章目录 选择排序法 - Selection Sort 为什么要学习O(n^2)的排序算法? 选择排序算法思想 操作:选择排序代码实现 选择排序法 - Selection Sort 简单记录-bobo ...
- 【算法】选择排序(Selection Sort)(二)
选择排序(Selection Sort) 选择排序(Selection-sort)是一种简单直观的排序算法.它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余 ...
- 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort
选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...
- 选择排序(Selection Sort)
选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...
随机推荐
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- git Bash下复制粘贴
git复制:Ctrl+insert git粘贴:Shift+Insert git常用快捷键链接地址:https://www.jianshu.com/p/cc1fbd89e087 在gitHup上下载他 ...
- 第 6 章 存储 - 044 - volume 生命周期管理
volume 生命周期管理 1)备份 因为 volume 实际上是 host 文件系统中的目录和文件,所以 volume 的备份实际上是对文件系统的备份 例如:本地的Registry,所有镜像都存在/ ...
- SSD固态硬盘是会掉速的。
也没什么好的办法. 只是自己不再疑神疑鬼,总觉得中病毒了. 下面的文章还是挺有参考意义的. http://diy.pconline.com.cn/627/6271636_all.html SSD变慢了 ...
- PC端、移动端的页面适配及兼容处理
转自 一.关于移动端兼容性 目前针对跨终端的方案,主要分为两大阵营:一套资源Vs两套资源. 第一种是通过响应式或页面终端判断去实现一套资源适配所有终端: 第二种是通过终端判断分别调取两套资源以适配所有 ...
- Lasso linear model实例 | Proliferation index | 评估单细胞的增殖指数
背景:We developed a cell-cycle scoring approach that uses expression data to compute an index for ever ...
- java日志及异常错误信息输出的问题
1.异常信息可以层层传递,直到最后一层再输出日志也来得及 2.错误信息要在发生错误的当时就输出日志,否则到了其它层,很难得到准确的错误信息内容
- 表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,使用过程的一个坑
一.mysql表结构中存在如下设计时 表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,如下 `updated_time` datetime NOT NU ...
- 20170922xlVBA_GetCellTextFromWordDocument
Sub GetCellTextFromWordDocument() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAler ...
- p2751 Job Processing
如果单单只安排过程1的时间最短,很容易算出来.用优先队列取最小,加上增量后再放回就行.对过程2也进行这样的操作.将过程1第一个完成的在过程2最后一个完成.以样例来说,过程1:1,1,2,2,3,过程2 ...