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

排序五 简单选择排序

时间:2015-03-06 15:24:31      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

要点
简单选择排序是一种选择排序

选择排序:每趟从待排序的记录中选出关键字最小的记录,顺序放在已排序的记录序列末尾,直到全部排序结束为止。

 

简单排序处理流程:

( 1 )从待排序序列中,找到关键字最小的元素;
( 2 )如果最小元素不是待排序序列的第一个元素,将其和第一个元素互换;
( 3 )从余下的 N - 1 个元素中,找出关键字最小的元素,重复( 1 )、( 2 )步,直到排序结束。
技术分享

如图所示,每趟排序中,将当前第 i 小的元素放在位置 i 上。

 

核心代码

public void selectionSort(int[] list) {
    // 需要遍历获得最小值的次数
    
// 要注意一点,当要排序 N 个数,已经经过 N-1 次遍历后,已经是有序数列
    for (int i = 0; i < list.length - 1; i++) {
        int temp = 0;
        int index = i; // 用来保存最小值得索引

        
// 寻找第i个小的数值
        for (int j = i + 1; j < list.length; j++) {
            if (list[index] > list[j]) {
                index = j;
            }
        }

        // 将找到的第i个小的数值放在第i个位置上
        temp = list[index];
        list[index] = list[i];
        list[i] = temp;
    }
}

 

 

算法分析
简单选择排序算法的性能
排序类别 排序方法 时间复杂度 空间复杂度 稳定性 复杂性
平均情况 最坏情况 最好情况
选择排序 简单选择排序
O(N2)
O(N2)
O(N2)
O(1) 不稳定 简单


时间复杂度

简单选择排序的比较次数与序列的初始排序无关。 假设待排序的序列有 N 个元素,则比较次数总是N (N - 1) / 2

而移动次数与序列的初始排序有关。当序列正序时,移动次数最少,为 0.

当序列反序时,移动次数最多,为3N (N - 1) /  2。

所以,综合以上,简单排序的时间复杂度为 O(N2)

 

空间复杂度

简单选择排序需要占用一个临时空间,在交换数值时使用。

 


完整参考代码
JAVA版本
代码实现
技术分享
 1 import java.util.Random;
 2 
 3 public class SelectionSort {
 4 
 5     public void selectionSort(int[] list) {
 6         // 需要遍历获得最小值的次数
 7         // 要注意一点,当要排序 N 个数,已经经过 N-1 次遍历后,已经是有序数列
 8         for (int i = 0; i < list.length - 1; i++) {
 9             int temp = 0;
10             int index = i; // 用来保存最小值得索引
11 
12             // 寻找第i个小的数值
13             for (int j = i + 1; j < list.length; j++) {
14                 if (list[index] > list[j]) {
15                     index = j;
16                 }
17             }
18 
19             // 将找到的第i个小的数值放在第i个位置上
20             temp = list[index];
21             list[index] = list[i];
22             list[i] = temp;
23             
24             System.out.format("第 %d 趟:\t", i + 1);
25             printAll(list);
26         }
27     }
28 
29     // 打印完整序列
30     public void printAll(int[] list) {
31         for (int value : list) {
32             System.out.print(value + "\t");
33         }
34         System.out.println();
35     }
36 
37     public static void main(String[] args) {
38         // 初始化一个随机序列
39         final int MAX_SIZE = 10;
40         int[] array = new int[MAX_SIZE];
41         Random random = new Random();
42         for (int i = 0; i < MAX_SIZE; i++) {
43             array[i] = random.nextInt(MAX_SIZE);
44         }
45 
46         // 调用冒泡排序方法
47         SelectionSort selection = new SelectionSort();
48         System.out.print("排序前:\t");
49         selection.printAll(array);
50         selection.selectionSort(array);
51         System.out.print("排序后:\t");
52         selection.printAll(array);
53     }
54 
55 }
简单选择排序之JAVA实现


运行结果

排序前:     5    6    1    6    1    1    5    5    4    2    
第 1 趟:    1    6    5    6    1    1    5    5    4    2    
第 2 趟:    1    1    5    6    6    1    5    5    4    2    
第 3 趟:    1    1    1    6    6    5    5    5    4    2    
第 4 趟:    1    1    1    2    6    5    5    5    4    6    
第 5 趟:    1    1    1    2    4    5    5    5    6    6    
第 6 趟:    1    1    1    2    4    5    5    5    6    6    
第 7 趟:    1    1    1    2    4    5    5    5    6    6    
第 8 趟:    1    1    1    2    4    5    5    5    6    6    
第 9 趟:    1    1    1    2    4    5    5    5    6    6    
排序后:     1    1    1    2    4    5    5    5    6    6



参考资料
《数据结构习题与解析》(B级第3版)

排序五 简单选择排序

标签:

原文地址:http://www.cnblogs.com/jingmoxukong/p/4303289.html

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