选择排序

来源:互联网 发布:信息处理技术员软件 编辑:程序博客网 时间:2024/06/15 20:28

选择排序:

       第一次在整个队列中选择最小的一个,如果这个最小的数不是在0的位置上,就把该数字与在0位置上的数字互换。之后以此类推,知道把整个队列都排序完成即可

public class SelectSort {public static void selectSort(int[] a){int length = a.length;int temp,k;for(int i = 0; i < length; i ++){temp = a[i];k = i;for(int j = i; j < length; j ++ ){if(a[j] < temp){temp = a[j];k = j;}}System.out.println(temp);}}public static void main(String args[]){int[] a = {9,3,6,1,7,2};selectSort(a);}}
选择算法的时间复杂度也是O(n*2)。但是其比执行语句的数量可以看出比冒泡算法还是要少的。

0 0
原创粉丝点击