码迷,mamicode.com
首页 > 编程语言 > 详细

选择排序

时间:2017-07-27 22:37:37      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:logs   ble   turn   select   log   ext   复杂度   return   new   

一、概念

每一趟从待排序的数据元素中选出最小(最大)的一个元素,放在已排好序的最前(最后),直到全部待排序的数据元素排完。

二、复杂度

排序方法 最差时间分析 最好时间分析 平均时间复杂度 空间复杂度 稳定性
选择排序 O(n2) O(n2) O(n2) O(1) 稳定

三、代码实现

 1     //选择排序算法
 2     public void selectsort(int[] array){
 3         if(array.length == 0 || array == null)
 4             return;
 5         //每次选择最小的到最前
 6         for(int i = 0; i < array.length; i++){
 7             int min = i;
 8             for(int j = i+1; j < array.length; j++){
 9                 if(array[min] > array[j])
10                     min = j;
11             }
12             if(i != min){
13                 int temp = array[min];
14                 array[min] = array[i];
15                 array[i] = temp;
16             }
17             printArray(array,i+1);
18         }
19     }
20     public void printArray(int a[],int count){
21         if(count != 0)
22         System.out.print("第" + count + "次   ");
23         for(int m = 0; m < a.length; m++){
24             if(count == m && count != 0)
25                 System.out.print("|");
26             System.out.print(a[m] + " ");
27         }
28         System.out.println();
29     }
30     public static void main(String[] args) {
31         int a[] = {11,7,6,1,8,4,3,2};
32         SelectSort bs = new SelectSort();
33         bs.selectsort(a);
34     }

技术分享

选择排序

标签:logs   ble   turn   select   log   ext   复杂度   return   new   

原文地址:http://www.cnblogs.com/fankongkong/p/7247500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!