zoukankan      html  css  js  c++  java
  • (转)排序算法之简单选择排序

    简单选择排序简介

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

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

    简单排序处理流程

    (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 趟:	", 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 趟:	", i + 1);
                printAll(list);
            }
        }
     
        // 打印完整序列
        public void printAll(int[] list) {
            for (int value : list) {
                System.out.print(value + "	");
            }
            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("排序前:	");
            selection.printAll(array);
            selection.selectionSort(array);
            System.out.print("排序后:	");
            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
  • 相关阅读:
    关于初学者Could not find action or result :No result defined for action com.lyw.action.LoginAction and result success
    tomcat运行时候出现java.net.BindException: Address already in use: JVM_Bind错误解决方法
    关于jQuery中环境配置中的问题
    myeclicps开发web时候复制一个工程改名字后执行出现404错误
    非正常关闭myeclicps后,出现错误Errors occurred during the build.的解决方法
    【软件工程】读《人月神话》有感
    【软件工程】一个学期软件工程课的感想
    【软件工程】“谁是卧底”之NABC分析
    [软件工程] 查找二维数组最大子数组的之和 郭莉莉&李亚文
    【软件工程】软件开发方法综述
  • 原文地址:https://www.cnblogs.com/wangxiayun/p/10038017.html
Copyright © 2011-2022 走看看