Seletion Sort
2015-10-06 05:13
267 查看
referrence: GeeksforGeeks
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
Time comlexity O(n^2), space cost O(1), and it is in-place sorting.
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
public static void SelectionSort ( int [ ] num ) { int i, j, min, minIndex; for ( i = 0; i < num.length; i++ ) { min = num[i]; //initialize min for(j = i; j < num.length; j ++) //inside loop { if( num[j] < min) { min = num[j]; minIndex = j; } } //swap min and num[i]; num[j] = num[i]; num[i] = min; } }
Time comlexity O(n^2), space cost O(1), and it is in-place sorting.
相关文章推荐
- 如何解决HTC Desire 826无法使用谷歌服务的问题
- 浅谈vector存储内容与空间利用
- App 推荐:Spotify
- Binary Search
- 去掉UITableView的Cell选中状态
- 如何策划一个符合seo标准的网站(详细思路)
- Linux 内核 0.01 发布纪念日:协作的价值
- 超越竞争对手上首页SEO优化方案
- android 界面控件被输入法顶起来解决
- 纯PHP实现定时器任务(Timer)
- 导航菜单栏制作
- MySQL常用查询语句
- Linux常见应用--DNS服务器常见应用实现
- PHP setcookie()用法
- Privileged containers provisioning by Kubernetes in Ubuntu
- PDO
- Windows 64位下安装Redis详细教程
- Leetcode Meeting Rooms
- DooDigestAuth php(后台)授权管理类 web浏览器授权
- *LeetCode-Path Sum II