博客
关于我
2.选择排序
阅读量:805 次
发布时间:2019-03-25

本文共 2797 字,大约阅读时间需要 9 分钟。

选择排序的时间复杂度是O(N²),而额外空间复杂度是O(1)。通过对比选择排序实现过程,我发现自己之前的实现方式存在一定的不足。

在开始编写代码之前,我需要明白选择排序的基本工作原理。选择排序每一趟比较中需要找到当前未排序部分中的最小值,并将其与已排序部分的第一个位置交换。这种方法的时间复杂度为O(N²),但是如果每次都直接比较并交换,会导致大量的资源浪费和时间消耗。

牛客老师的实现方法更为高效。老师采用了一个局部变量来记录每趟比较中最小值的下标。在每一趟比较中,只需要遍历当前未排序的部分,就可以找到最小值的位置。通过这种方法,每一趟比较的时间复杂度降到了O(N),而整个算法的时间复杂度就降到了O(N²)。这种比直接对比更优化的做法,使得整体性能得到了明显提升。

以下是我的两种实现方法的对比:

第一种方法(较慢):

public static void SelectSort2(int[] arr) {    if (arr == null || arr.length < 2) {        return;    }    for (int i = 0; i < arr.length; ++i) {        for (int j = i + 1; j < arr.length; ++j) {            if (arr[j] < arr[i]) {                swap(arr, i, j);            }        }    }}

第二种方法(牛客老师的方法):

public static void SelectSort(int[] arr) {    if (arr == null || arr.length < 2) {        return;    }    for (int i = 0; i < arr.length; ++i) {        int minIndex = i;        for (int j = i + 1; j < arr.length; ++j) {            if (arr[j] < arr[minIndex]) {                minIndex = j;            }        }        swap(arr, i, minIndex);    }}

通过对比这两种方法,我意识到自己之前的实现方式在每一趟比较中都进行了直接的交换操作,这种方法虽然简单,但却在每一趟比较中进行了大量不必要的比较,导致了性能下降。相比之下,牛客老师的方法通过提前找到每趟比较中的最小值位置,只需一次交换操作,就能将最小值放到正确的位置,从而优化了性能。

接下来,我将这些实现细节总结在代码中,供其他开发者参考和学习。

完整代码:

package yzy.algorithm;import java.util.Arrays;public class testSelectSort {    public static void main(String[] args) {        int[] values = new int[10];        for (int i = 0; i < 10; ++i) {            values[i] = (int) (10 * Math.random());        }        System.out.println(Arrays.toString(values));        SelectSort(values);        System.out.println(Arrays.toString(values));        int[] values2 = new int[10];        for (int i = 0; i < 10; ++i) {            values2[i] = (int) (10 * Math.random());        }        System.out.println(Arrays.toString(values2));        SelectSort2(values2);        System.out.println(Arrays.toString(values2));    }    // 牛客老师做法    public static void SelectSort(int[] arr) {        if (arr == null || arr.length < 2) {            return;        }        for (int i = 0; i < arr.length; ++i) {            int minIndex = i;            for (int j = i + 1; j < arr.length; ++j) {                if (arr[j] < arr[minIndex]) {                    minIndex = j;                }            }            swap(arr, i, minIndex);        }    }    public static void swap(int[] arr, int i, int j) {        int tmp = arr[i];        arr[i] = arr[j];        arr[j] = tmp;    }    // 以往自己的写法    public static void SelectSort2(int[] arr) {        if (arr == null || arr.length < 2) {            return;        }        for (int i = 0; i < arr.length; ++i) {            for (int j = i + 1; j < arr.length; ++j) {                if (arr[j] < arr[i]) {                    swap(arr, i, j);                }            }        }    }}

通过实践对比,我深刻地理解了选择排序的实现要点。希望这些内容能为其他开发者提供参考。

转载地址:http://sldyk.baihongyu.com/

你可能感兴趣的文章
python中列表乘法、eval、lambda、hasattr、assert、*(星)等的用法
查看>>
Python中制作词云的WordCloud参数详解
查看>>
Python中可用和使用的系统内存?
查看>>
python中存根文件(.pyi)的用途是什么?
查看>>
Python中实现ASCII码与字符相互转换
查看>>
Python中实现获取所有微信好友的头像并拼接成一张图片
查看>>
Python中对象的布尔值
查看>>
python中对集合的操作
查看>>
python中将列表转换为元组的时间复杂度,反之亦然
查看>>
Python中布尔表达式的求值
查看>>
python中很常用的10个内置函数整理(初学必备)
查看>>
python中打印二维列表数据
查看>>
python中打散数组shuffle
查看>>
Python中执行外部命令
查看>>
Python中拼接numpy数组-ChatGPT4o作答
查看>>
Python中提示:no module named 'PIL'
查看>>
Python中提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
查看>>
python中数据类型存储_浅谈Python中的数据类型
查看>>
python中文件最重要的功能是什么-学 Python 都用来干嘛的?
查看>>
python中文版语言_高盛最新调查:Python超过汉语成为未来最重要技能,你准备学哪种编程语言?...
查看>>