排序算法——选择排序(Selection Sort)
来源:互联网 发布:软件风险管理报告 编辑:程序博客网 时间:2024/06/16 13:25
排序算法——选择排序(Selection Sort)
算法简介(Introduction)
We start selection sort by scanning entire given list to find its smallest element and exchange it with the first element, putting the smallest element in its final position in sorted list. Then we can scan the list, starting with the second element to find the smallest element among the last n-1 elements and exchange it with the second element, putting the second smallest element in its final position. Basically, in the ith scanning among last n-1-i elements, find the smallest one and exchange with ith element in the list.
Left elements has been sorted. Exchange A[i] with A[min].
伪代码(Pseudocode)
function SelectionSort(A[0..n-1]) for i ⟵ 0 to n-2 do min ⟵ i for j ⟵ i+1 to n-1 do if A[j] < A[min] then min ⟵ j swap A[i] and A[min]
基本属性(property)
Input: an array A[0..n-1] of n order able items, such as numbers, characters, character strings etc.
Output: an array A[0..n-1] sorted in non-descending order.
In-place: YES. Only requires a constant amount O(1) of additional memory space. Just a little memory for variable i, j, min, tmp(for swap).
Stable: YES. Does not change the relative order of elements with equal keys. For example, A[3]=A[15]=100. The relative order of A[3] and A[15] does not change, which is A[3] is always on the left of A[15].
时间复杂度(Time Complexity)
The size of input is n.
The basic operation is key comparison A[j] < A[min].
The number of times basic operation performed id denoted as Cn.
Hence, selection sort is quadratic time.
适用情形(Suitable Situation)
Selection sort performs inefficiently on large list compared with insertion sort due to its O(n^2) time complexity. But it is noted for its simplicity. It is a well-known brute force algorithm.
Java Code
public class Sort{ //Selection sort method public static int[] selectionSort(int[] A){ int i,j,min,tmp; for(i=0;i<A.length-1;i++){ min=i; for(j=i+1;j<A.length;j++) if(A[j]<A[min]) min=j; tmp=A[i]; A[i]=A[min]; A[min]=tmp; } return A; } //Test public static void main(String[] args){ int[] A={10,15,32,87,90,100,52,67}; int[] sortedA=Sort.selectionSort(A); for(int i=0;i<sortedA.length;i++) System.out.print(sortedA[i]+" "); }}
运行结果(Result)
10 15 32 52 67 87 90 100
写在最后的话(PS)
If you have any question for me, please contact me. My email address is shuaiw6@student.unimelb.edu.au.
- 选择排序算法——Selection Sort
- 排序算法——选择排序(Selection Sort)
- 排序算法——选择排序(Selection Sort)
- 算法--选择排序 Selection Sort
- 经典排序算法 - 选择排序Selection sort
- 经典排序算法 - 选择排序Selection sort
- 经典排序算法 - 选择排序Selection sort
- 排序算法-选择排序 Selection Sort
- 经典排序算法 - 选择排序Selection sort
- 排序算法---选择排序(Selection Sort)
- 选择排序—简单选择排序(Simple Selection Sort)
- 选择排序—简单选择排序(Simple Selection Sort)
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- selection sort 选择排序
- 选择排序 Selection Sort
- 选择排序(Selection Sort)
- 选择排序(Selection Sort)
- java内部类学习
- 谈谈我对Linux系统学习的历程回顾
- java中的Timer用法(三) 似乎可以使用了,可以上线了
- DSP笔记
- hadoop.zookeeper.hbase的hbase中master和regionserver无法启动
- 排序算法——选择排序(Selection Sort)
- XML
- MTK task
- Array和Arrays
- 字节码指令集
- MySql5.7WindowsUTF-8编码
- springMVC基于注解的权限控制
- Java使用阿里大于发送短信通知和短信验证码
- DOM