排序算法-SelectSort-选择排序
来源:互联网 发布:java开发cs架构的优点 编辑:程序博客网 时间:2024/06/10 13:16
1. 选择排序算法介绍
每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。选择排序是不稳定的排序方法。时间负责度为O(n^2)。
2. 算法实现
2.1 选择排序<由高到低>
//type * pArray: 待排序数组指针//int len:数组的长度//typename type:模板库template <typename type>void SelectSortHigh2Low(type * pArray, int len);template <typename type>void SelectSortHigh2Low(type * pArray, int len){ int i, j; int temp; for (i=0; i<len-1; i++) { temp = i; for (j=i+1; j<len; j++) { if (pArray[temp]<pArray[j]) temp = j; } if (temp!=i) Swap(&pArray[i], &pArray[temp]); }}
2.2 选择排序<由低到高>
//type * pArray: 待排序数组指针//int len:数组的长度//typename type:模板库template <typename type>void SelectSortlow2High(type * pArray, int len);template <typename type>void SelectSortlow2High(type * pArray, int len){ int i, j; int temp; for (i=0; i<len-1; i++) { temp = i; for (j=i+1; j<len; j++) { if (pArray[temp]>pArray[j]) temp = j; } if (temp!=i) Swap(&pArray[i], &pArray[temp]); }}
2.3 交换数值
template <typename type>void Swap(type * lhs, type * rhs){ type temp = *lhs; *lhs = *rhs; *rhs = temp;}
3. 示例
在主函数中输入10个等长的字符串(每个字符串最多10个字符),对他们进行由大到小的排序,然后再主函数进行输出结果。
输入
10个等长的字符串,用空格分隔。
输出
输出排序后的字符串
样例:
输入: she its can ibm bbc NBA nhk BOY jxf eat
输出: BOY NBA bbc can eat ibm its jxf nhk she
//数组定义:void arrary[3][10];//连续型的数组定义void SelectSortLow2High(char pArray[][10], int len){ int i, j; int temp; for (i=0; i<len-1; i++) { temp = i; for (j=i+1; j<len; j++) { if ( memcmp(pArray[temp], pArray[j], strlen(pArray[j])) > 0 ) temp = j; } if (temp!=i) Swap(pArray[i], pArray[temp]); }}void Swap(char * lhs, char * rhs){ char temp[10] = {'\0'}; memcpy(temp, lhs, strlen(lhs)); memcpy(lhs, rhs, strlen(rhs)); memcpy(rhs, temp, strlen(temp));}
0 0
- 排序算法-SelectSort-选择排序
- [算法 java] 选择排序selectSort
- 选择排序:selectSort
- selectSort - 选择排序
- SelectSort,选择排序
- 选择排序SelectSort
- 选择排序-selectSort
- 选择排序--SelectSort
- 选择排序-selectsort
- 选择排序(SelectSort)
- 选择排序:selectSort
- 选择排序(SelectSort)
- SelectSort 选择排序
- 【排序算法】直接选择排序(SelectSort)与冒泡排序(BubbleSort)
- Java--选择排序(SelectSort)
- 【数据结构】中的选择(SelectSort)排序
- 选择排序(selectSort.cpp)
- 数据算法之选择排序(selectSort)的Java实现
- sharedUserId 区别 process
- 基于nginx的api网关
- 自学QT之常量中有换行符错误的解决办法
- 【计蒜客系列】挑战难题20:跳跃游戏二
- SWT学习(一)
- 排序算法-SelectSort-选择排序
- ScrollView嵌套GridView或者是ListView,导致GridView或者是ListView显示不完整
- 如何使用Telnet远程访问测试端口
- crontab
- iOS安全–使用static inline方式编译函数,防止静态分析
- SWT学习(二)
- 常用技巧记录
- 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
- Java工程师成神之路~