(转)排序算法之简单选择排序

简单选择排序简介

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

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

简单排序处理流程

(1)从待排序序列中,找到关键字最小的元素;

(2)如果最小元素不是待排序序列的第一个元素,将其和第一个元素互换;

(3)从余下的 N - 1 个元素中,找出关键字最小的元素,重复(1)、(2)步,直到排序结束。

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;

 

        System.out.format("第 %d 趟:\t", i + 1);

        printAll(list);

    }

}

 

算法分析

简单选择排序算法的性能

排序类别

排序方法

时间复杂度

空间复杂度

稳定性

复杂性

平均情况

最坏情况

最好情况

选择排序

简单选择排序

O(N2)

O(N2)

O(N2)

O(1)

不稳定

简单

 

 

时间复杂度

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

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

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

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

空间复杂度

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

 

完整参考代码

package notes.javase.algorithm.sort;
 
import java.util.Random;
 
public class SelectionSort {
 
    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;
 
            System.out.format("第 %d 趟:\t", i + 1);
            printAll(list);
        }
    }
 
    // 打印完整序列
    public void printAll(int[] list) {
        for (int value : list) {
            System.out.print(value + "\t");
        }
        System.out.println();
    }
 
    public static void main(String[] args) {
        // 初始化一个随机序列
        final int MAX_SIZE = 10;
        int[] array = new int[MAX_SIZE];
        Random random = new Random();
        for (int i = 0; i < MAX_SIZE; i++) {
            array[i] = random.nextInt(MAX_SIZE);
        }
 
        // 调用冒泡排序方法
        SelectionSort selection = new SelectionSort();
        System.out.print("排序前:\t");
        selection.printAll(array);
        selection.selectionSort(array);
        System.out.print("排序后:\t");
        selection.printAll(array);
    }
 
}

运行结果

排序前:   3  5  2  8  1  2  0  8  4  11 趟:  0  5  2  8  1  2  3  8  4  12 趟:  0  1  2  8  5  2  3  8  4  13 趟:  0  1  1  8  5  2  3  8  4  24 趟:  0  1  1  2  5  8  3  8  4  25 趟:  0  1  1  2  2  8  3  8  4  56 趟:  0  1  1  2  2  3  8  8  4  57 趟:  0  1  1  2  2  3  4  8  8  58 趟:  0  1  1  2  2  3  4  5  8  89 趟:  0  1  1  2  2  3  4  5  8  8 

排序后:   0  1  1  2  2  3  4  5  8  8

 

转载于:https://www.cnblogs.com/wangxiayun/p/10038017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值