用模板写选择排序-数组
2014-09-06 15:39
162 查看
今天,我们一起来写选择排序,主要是熟悉模板编程,具体如例1所示。
例1 选择排序-数组
SelectSort.hpp内容:
图1 运行效果
今天,我们一起完成了选择排序,希望对家回去实践一下,加深体会。
例1 选择排序-数组
SelectSort.hpp内容:
#ifndef _SELECT_SORT_H_ #define _SELECT_SORT_H_ template<typename T> bool SelectSort(T * pInput, int nLen) { int i = 0; int j = 0; int nMin = 0; T tTemp; if (!pInput) return false; for (i = 0; i < nLen; i++) { nMin = i; for (j = i + 1; j < nLen; j++) { if (pInput[j] < pInput[nMin]) { nMin = j; } } if (nMin != i) { tTemp = pInput[i]; pInput[i] = pInput[nMin]; pInput[nMin] = tTemp; } } return true; } #endifmain.cpp的内容:
#include "SelectSort.hpp" #include <iostream> using namespace std; void main() { int i = 0; int a[10] = { 1, 4, 7, 2, 5, 8, 3, 6, 9, 0 }; cout << "排序前:" << endl; for (i = 0; i < 10; i++) { cout << a[i] << '\t'; } cout << endl; if (SelectSort<int>(a, 1) == false) { cout << "排序失败." << endl; } else { cout << "排序后:" << endl; for (i = 0; i < 10; i++) { cout << a[i] << '\t'; } } system("pause"); return; }运行效果如图1所示:
图1 运行效果
今天,我们一起完成了选择排序,希望对家回去实践一下,加深体会。
相关文章推荐
- 用模板写选择排序-数组
- 数组排序总结(冒泡,选择,插入,希尔)
- Java数组排序总结(冒泡,选择,插入,希尔)
- 数组最大值,选择排序,冒泡排序
- 数组的选择--固定大小数组模板array存在的意义!
- 数组的选择排序算法和冒泡排序算法
- 怎样编写一个程序,把一个有序整数数组放到二叉树中? 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
- 数据结构-04 数组 -选择排序
- Java数组排序总结(冒泡,选择,插入,希尔)
- c+两种简单的数组排序,冒泡排序和类似选择排序的思考
- Java数组排序总结(冒泡,选择,插入,希尔)
- Java实现数组排序总结篇(冒泡,选择,插入,希尔)
- 浅谈java中整型数组选择排序的思想
- C#学习笔记—数组的选择排序
- 选择排序算法---对一个数组进行选择排序
- 【转】Java数组排序总结(冒泡,选择,插入,希尔)
- Java数组排序(冒泡,选择,插入,希尔)
- C语言数组实现冒泡排序和选择排序程序
- Java数组排序总结(冒泡,选择,插入,希尔)
- 十、数组的排序(冒泡排序,选择排序)