选择排序(selection sort)
来源:互联网 发布:中国网络声优排行榜 编辑:程序博客网 时间:2024/06/05 12:02
选择排序是从所有序列中先找到最小的,然后放到第一个位置。之后再看剩余元素中最小的,放到第二个位置……以此类推,就可以完成整个的排序工作了。
寻找最小的元素需要一个循环的过程,而排序又是需要一个循环的过程。因此,算法的时间复杂度也是O(n^2)的。在n比较小的情况下,算法可以保证一定的速度,当n足够大时,算法的效率会降低。并且随着n的增大,算法的时间增长很快。因此使用时需要特别注意。
这里写代码片#include<stdio.h>#include<stdlib.h>void array_show(int* arr, int len);template<class T>void SelectSort(T a[], int len);int main(int argc, char* argv[]){ int arr[10] = {4,3,8,5,2,1,6,0,7,9}; array_show(arr, 10); SelectSort(arr, 10); putchar(10); array_show(arr, 10); system("pause"); return 0;}template<class T>void SelectSort(T a[], int len){ T temp; int Index=0; //每次循环只进行一次交换 最多进行len-1次循环,因此总体上,比冒泡进行交换的次数少 for (int i=0;i<len-1;i++) { //第i次排序时,已经进行了i次大循环,因此已经排好了i个元素 //已排好序的元素0,,...,i-2,i-1 //待排元素为i,i+1,...,len-1 Index=i; for (int j=i+1;j<len;j++) { if (a[j]<a[Index]) { Index=j; } } //交换 if (Index!=i) { temp=a[i]; a[i]=a[Index]; a[Index]=temp; } }}
选择排序:
平均时间复杂度:O(n2)
空间复杂度:O(1) (用于交换和记录索引)
稳定性:不稳定
0 0
- 选择排序(selection sort)
- 选择排序(selection sort)
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- 选择排序(Selection Sort)
- 选择排序(selection sort)
- Selection Sort(选择排序)
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- selection sort 选择排序
- 选择排序 Selection Sort
- 选择排序(Selection Sort)
- 选择排序(Selection Sort)
- 选择排序 (Selection Sort)
- 选择排序(Selection sort)
- 选择排序 (Selection sort)
- 选择排序Selection sort
- Selection Sort(选择排序)
- 前言
- 杭电 HDU 1247 ACMHat’s Words(trie树 或着STL)
- 使用testng listener实现测试用例失败重跑功能
- 什么是事务的传播特性?
- LeetCodeOJ_168_Excel Sheet Column Title
- 选择排序(selection sort)
- Linux关机命令详解
- PAT (Advanced Level) 1011. World Cup Betting (20) 求最大期望值,简单题
- SharePoint 2013 开发——获取用户配置文件属性内容(User Profile)
- 记大二结束
- 数据结构二叉树
- Maven学习之搭建私服Nexus
- Eclipse从数据库逆向生成Hibernate带注解的实体类
- 【转载】缓存淘汰算法--LRU算法