小仙女讲软考(四):选择排序
来源:互联网 发布:彩票自动分析软件 编辑:程序博客网 时间:2024/04/27 15:49
如果说插入排序是这样:
那选择排序就是这样:
选择排序分为简单选择排序和堆排序。这两个都较简单。
简单选择
定义:每一趟在n-i+1(i=1,2,…n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录。
特点:遍历;交换
图解:
[图解1]
[图解2]
代码实现:
public void SimpleSelectSort(int[] array){ int tmp=0; int t=0;//最小数标记 for(int i=0; i<array.Length; i++) { t=i; for(int j=i+1; j<array.Length; j++) { if(array[t]>array[j]) { t=j; }}tmp=array[i];array[i]=array[t];array[t]=tmp;
堆排序
定义:
利用堆这种数据结构所设计的一种排序算法。其可以利用数组的特点快速定位指定索引的元素。堆是完全二叉树,分为大根堆和小根堆。
据说此排序在面试中出现频率很高。
特点:堆;数组
实现步骤:(以大根堆为例)
1将本来无序堆调整为有序的大根堆
调整为有序:
2取出堆顶元素,将末尾元素补到堆顶
3重复步骤1
代码实现
///<summary>///构建堆 ///</summary> static void HeapAdjust(List<int> list,int parent,int length) { int temp=list[parent]; int child=2*parent+1; while(child<length){if(child+1<length&&list[child]<list[child+1])child++; if(temp>=list[child])break; list[parent]=list[child]; parent=child; child=2*parent+1;} list[parent]=temp;} ///<summary>///堆排序///</summary>public static List<int> HeapSort(List<int> list,int top){List<int> topNode=new List<int>(); for(int i=list.Count/2-1;i>=0;i--){HeapAdjust(list,i,list.Count);} for(int i=list.Count-1;i>=list.Count-top;i--){int temp=list[0];list[0]=list[i];list[i]=temp; topNode.Add(temp); HeapAdjust(list,0,i);}return topNode;}
小结
选择排序是冒泡排序的优化
阅读全文
0 0
- 小仙女讲软考(四):选择排序
- 小仙女讲软考(三):插入排序
- 小仙女讲软考(五):系统开发和运行
- 小仙女讲软考(六):说说测试那些事儿
- 选择排序(四)
- 小仙女-Jquery基础
- 小仙女-mybatis基础
- 小仙女大梦想
- 小仙女是谁???
- 小仙女讲软考之算法设计和分析
- 小仙女讲软考(二):全书各章节的联系
- 小仙女—过滤器(filter)、监听器(listener)
- 小仙女-js基础精华
- 自学篇-选择排序(四)
- 数据结构之选择排序(四)
- 排序算法四:选择排序
- 排序(四)---简单选择排序
- 排序算法(四) - 选择排序
- notepad++树形目录形式查看Java项目
- 改变alertview的字体颜色
- unity小白的资源
- Android关于Theme.AppCompat相关问题的深入分析
- 小波分析 + 支持向量机(SVM)预测股票涨跌幅的实现作者:k474905973
- 小仙女讲软考(四):选择排序
- 017、创建对象做了哪些事情
- netty 简单服务端和客户端
- 如何写SysV服务管理脚本
- 【Android前端】Bing每日图片列表应用制作记录——4.获取网络数据(下)
- Android添加依赖出现This support library should not use a different version (26) than the compile(25).
- DroneKit教程(一):安装DroneKit和测试工具
- android 部分手机修改led灯方法
- MYSQL高级命令