标签:
1 package sorting; 2 3 /** 4 * 选择排序 5 * 平均O(n^2),最好O(n^2),最坏O(n^2);空间复杂度O(1);不稳定;简单 6 * @author zeng 7 * 8 */ 9 public class XuanzePaixu { 10 11 public static int[] xuanzepaixu(int[] a){ 12 int i,j,n=a.length; 13 int tmp; 14 int k =0; 15 for(i=0;i<n;i++){ 16 k=i; 17 //找出最小值的小标 18 for(j=i+1;j<n;j++){ 19 if(a[j]<a[k]){ 20 k = j; 21 } 22 } 23 //将最小值放到排序序列末尾 24 tmp = a[i]; 25 a[i]=a[k]; 26 a[k]=tmp; 27 } 28 return a; 29 } 30 31 public static void main(String[] args) { 32 int[] b = { 49, 38, 65, 97, 76, 13, 27, 50 }; 33 xuanzepaixu(b); 34 for (int i : b) 35 System.out.print(i+" "); 36 37 } 38 }
标签:
原文地址:http://www.cnblogs.com/zengzhihua/p/4456741.html