数据结构 - 只需选择排序(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 中排序算法,之前也都看过它们的实现,因为没有深入分析的缘故,一直记不住谁是谁,本文就记录一下我学习的一些心得. 三种排序算法可以总结为如下: 都将数组分为已排序部分和未排序部分 ...
随机推荐
- 【t086】防护伞
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 据说2012的灾难和太阳黑子的爆发有关.于是地球防卫小队决定制造一个特殊防护伞,挡住太阳黑子爆发的区域 ...
- LVS负载均衡+动静分离+高可用(nginx+tomcat+keepalived)
文章目录 [隐藏] 一.环境介绍 二.环境安装 1.安装JDK 2.两台服务器安装tomcat 3.nginx安装 4.keepalive安装 三.负载均衡 四.动静分离 五.keepalive高可用 ...
- stm32的pwm波
- 【u121】教主的花园
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都 ...
- [Recompose] Add Local State with Redux-like Reducers using Recompose
Learn how to use the 'withReducer' higher order component using the alternative reducer form. If you ...
- [RxJS] Flatten a higher order observable with concatAll in RxJS
Besides switch and mergeAll, RxJS also provides concatAll as a flattening operator. In this lesson w ...
- [GraphQL] Write a GraphQL Mutation
In order to change the data that we can query for in a GraphQL Schema, we have to define what is cal ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- 【U218】A-B
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给出一个A/B-C/D表示的分数减法算式,A,B,C,D均为不超过32767的正整数,求A/B-C/D ...
- springmvc中controller内方法跳转forward?redirect?
使用springmvc的controller的时候,碰到controller内方法的跳转的问题,记录下问题以及自己测试的过程. 场景: 业务执行更新操作之后返回列表页面,列表页面需默认展示查询的列表数 ...