数据结构 - 只需选择排序(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 中排序算法,之前也都看过它们的实现,因为没有深入分析的缘故,一直记不住谁是谁,本文就记录一下我学习的一些心得. 三种排序算法可以总结为如下: 都将数组分为已排序部分和未排序部分 ...
随机推荐
- 在Eclipse中运行Nutch2.3 分类: H3_NUTCH 2015-01-28 16:41 3175人阅读 评论(13) 收藏
参考http://wiki.apache.org/nutch/RunNutchInEclipse 一.环境准备 1.下载nutch2.3源代码 wget http://mirror.bit.edu.c ...
- angular之Http服务
原文 https://www.jianshu.com/p/53e4a4bfad7d 大纲 1.什么是angular服务 2.服务的类别 3.认识angular的Http请求 4.简单实例 5.angu ...
- wordpress-nas
- 驱动程序调试方法之printk——printk的原理与直接使用
1.基本原理 (1)在UBOOT里设置console=ttySAC0或者console=tty1 这里是设置控制终端,tySAC0 表示串口, tty1 表示lcd(2)内核用printk打印 内核就 ...
- javascript数组全排列,数组元素所有组合
function permute(input) { var permArr = [], usedChars = []; function main(input){ var i, ch; for (i ...
- html5 在移动端的缩放控制
viewport 语法介绍: 01 <!-- html document --> 02 <meta name="viewport" 03 content= ...
- Visual Stdio 环境下使用 GSL (GNU Scientific Library)
Visual Stdio 环境下使用 GSL (GNU Scientific Library) 经測试.这里的方法不适用于VS2015. * 这篇文章有点过时了.建议从以下网址下载能够在 vs 环境下 ...
- [Node.js] Create a model to persist data in a Node.js LoopBack API
In this lesson you will learn what a LoopBack model is, you will create a Product model using the Lo ...
- [Compose] 14. Build curried functions
We see what it means to curry a function, then walk through several examples of curried functions an ...
- CSDN编程挑战——《交替字符串》
交替字符串 题目详情: 假设字符串str3可以由str1和str2中的字符按顺序交替形成,那么称str3为str1和str2的交替字符串.比如str1="abc",str2=&qu ...