博客
关于我
每日轻松学算法(选择)
阅读量:655 次
发布时间:2019-03-15

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

1. 原理

  • 选择排序(Selection-sort)是一种简单直观的排序算法。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

2. 思路

在这里插入图片描述

3. 动图演示

在这里插入图片描述

4. 代码实现

package cn.itcast;public class SelectionSort {       public static void main(String[] args) {           //定义整形数组        int arr[]={   3,38,5,44,15,36,26,27,2,47,46,4,19,50,48};        //调用选择排序方法        int arrays[]=selectionSort(arr);        //遍历输出结果        for (int a : arrays) {               System.out.print(a+" ");        }    }    //选择排序    public static int[] selectionSort(int[] array){           if(array.length==0)            return array;        for (int i = 0; i < array.length; i++) {               //初始化最小值下标            int minIndex=i;            for(int j=i;j

运行结果

在这里插入图片描述

5. 算法分析

最佳情况:T(n) = O(n2) 最差情况:T(n) = O(n2) 平均情况:T(n) = O(n2)

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

你可能感兴趣的文章
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>