您现在的位置是:主页 > news > 宿松 做网站/免费建设个人网站

宿松 做网站/免费建设个人网站

admin2025/5/31 13:42:04news

简介宿松 做网站,免费建设个人网站,最权威的品牌排行榜网站,html布局模板数据结构 - 只需选择排序(simple selection sort)本文地址: http://blog.csdn.net/caroline_wendy/article/details/28601965选择排序(selection sort) : 每一趟在n-i1个记录中选取keyword最小的记录作为有序序列中第i个记录.简单选择排序(simple selection sort) : 通过n-i次k…

宿松 做网站,免费建设个人网站,最权威的品牌排行榜网站,html布局模板数据结构 - 只需选择排序(simple selection sort)本文地址: http://blog.csdn.net/caroline_wendy/article/details/28601965选择排序(selection sort) : 每一趟在n-i1个记录中选取keyword最小的记录作为有序序列中第i个记录.简单选择排序(simple selection sort) : 通过n-i次k…

数据结构 - 只需选择排序(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 





版权声明:本文博主原创文章,博客,未经同意不得转载。