数据结构 - 只需选择排序(simple selection sort) 详细说明 和 代码(C++)
数据结构 - 只需选择排序(simple selection sort)
本文地址: http://blog.csdn.net/caroline_wendy/article/details/28601965
选择排序(selection sort) : 每一趟在n-i+1个记录中选取keyword最小的记录作为有序序列中第i个记录.
简单选择排序(simple selection sort) : 通过n-i次keyword之间的比較, 从n-i+1个记录中选出keyword最小的记录, 并和第i个记录交换.
选择排序须要比較n(n-1)/2次, 即(n-1)+(n-2)+...+1 = [(n-1)+1](n-1)/2次, 时间复杂度是O(n^2).
简单选择排序的主要步骤是: 1. 选出较小元素的位置. 2. 交换.
代码:
/*
* SimpleSelectionSort.cpp
*
* Created on: 2014.6.5
* Author: Spike
*/ #include <iostream>
#include <vector> void print(const std::vector<int>& L) {
for (auto i : L) {
std::cout << i << " ";
}
std::cout << std::endl;
} int SelectMinKey(std::vector<int>& L, const size_t p) {
int min = p;
for (size_t i=p+1; i<L.size(); ++i) {
if (L[i] < L[min]) {
min = i;
}
} return min;
} void SelectSort (std::vector<int>& L) {
for (size_t i=0; i<L.size(); ++i) {
size_t j = SelectMinKey(L, i);
if (i != j) std::swap(L[i], L[j]);
print(L);
}
} int main(void) {
std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
SelectSort(L);
print(L); }
输出:
4 38 65 97 76 13 27 49 55 49
4 13 65 97 76 38 27 49 55 49
4 13 27 97 76 38 65 49 55 49
4 13 27 38 76 97 65 49 55 49
4 13 27 38 49 97 65 76 55 49
4 13 27 38 49 49 65 76 55 97
4 13 27 38 49 49 55 76 65 97
4 13 27 38 49 49 55 65 76 97
4 13 27 38 49 49 55 65 76 97
4 13 27 38 49 49 55 65 76 97
4 13 27 38 49 49 55 65 76 97
版权声明:本文博主原创文章,博客,未经同意不得转载。
数据结构 - 只需选择排序(simple selection sort) 详细说明 和 代码(C++)的更多相关文章
- 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...
- 简单选择排序(Simple Selection Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现
选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...
- 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)
摘要 选择排序的逻辑是先遍历比较出序列中最大的,然后把最大的放在最后位置. 遵循这个逻辑,用代码实现时,做到1.减少比较次数之外,这里引入一个新的指标 - 稳定性,2.保证排序过程中的稳定性也是一个优 ...
- 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort
选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...
- 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结
背景 这两天温习了 5 中排序算法,之前也都看过它们的实现,因为没有深入分析的缘故,一直记不住谁是谁,本文就记录一下我学习的一些心得. 三种排序算法可以总结为如下: 都将数组分为已排序部分和未排序部分 ...
随机推荐
- [AngularFire 2] Joins in Firebase
Lets see how to query Firebase. First thing, when we do query, 'index' will always help, for both SQ ...
- 【物理/数学】—— 概念的理解 moment、momentum
moment:矩,momentum:[物] 动量:动力:冲力: 数学意义上的 moment(矩)概念其实源自于物理范畴.首先我们来介绍物理学意义上的矩(Momentum)的概念. 1. 物理学意义上的 ...
- [Node.js] Use nodejs-dashboard event loop delay with hrtime()
In this lesson, you will learn how to use the Formidable nodejs-dashboard event loop delay to identi ...
- 仿凤凰时时彩代购平台源代码[ASP+MSSQL]完整下载
源代码简单介绍 : 适用范围: 时时彩源代码,时时彩程序,开奖平台源代码,投注平台源代码,仿凤凰时时彩源代码 执行环境: ASP+MSSQL 其它说明:仿凤凰时时彩代购平台源代码.网上售价8000 ...
- MRTG Monitoring with ESXi Hosted Guest Return ‘interface is commented * has no ifSpeed property’
MRTG Monitoring with ESXi Hosted Guest Return ‘interface is commented * has no ifSpeed property’ Rec ...
- 我眼中的c++编程总结-20150602
断断续续的学习了非常多东西,有51.Avr.ARM.PLC.C\C++.C#.TB.MC.mql4.linux....等等,近乎填鸭或者囫囵吞枣的.甚至饿狼般的扑到里面,慢慢的积累和理解中,非常多知识 ...
- jquery平滑滚动页面
滚动到顶部 $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); 滚动到指定位置 ...
- 【u010】银河英雄传说
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开 ...
- Thinkphp5 Auth权限认证
Thinkphp5 Auth权限认证 一.总结 一句话总结:四表两组关系,一个多对多(权限和用户组之间),一个一对多(用户和用户组之间) 二.Thinkphp5 Auth权限认证 auth类在thin ...
- php的标准输入与输出是什么?
php的标准输入与输出是什么? 一.总结 php的标准输入与输出(STDIN是一个文件句柄,等同于fopen("php://stdin", 'r')) 1.STDIN是一个文件句柄 ...