当前位置: 首页 > news >正文

读书笔记-《数据结构与算法》-摘要3[选择排序]

选择排序

核心:不断地选择剩余元素中的最小者。

  1. 找到数组中最小元素并将其和数组第一个元素交换位置。
  2. 在剩下的元素中找到最小元素并将其与数组第二个元素交换,直至整个数组排序。

性质:

  • 比较次数=(N-1)+(N-2)+(N-3)+…+2+1~N^2/2
  • 交换次数=N
  • 运行时间与输入无关
  • 数据移动最少

在这里插入图片描述

public class SelectioSort {public static void main(String[] args) {int unsortedArray[] = new int[]{8, 5, 2, 6, 9, 3, 1, 4, 0, 7};selectionSort(unsortedArray);System.out.println("After sort: ");for (int item : unsortedArray) {System.out.print(item + " ");}}public static void selectionSort(int[] array) {int len = array.length;for (int i = 0; i < len; i++) {for (int item : array) {System.out.print(item + " ");}System.out.println();int min_index = i;// 将最小值的下标找出来for (int j = i + 1; j < len; j++) {if (array[j] < array[min_index]) {min_index = j;}}int temp = array[min_index];array[min_index] = array[i];array[i] = temp;}}
}

输出:

8 5 2 6 9 3 1 4 0 7 
0 5 2 6 9 3 1 4 8 7 
0 1 2 6 9 3 5 4 8 7 
0 1 2 6 9 3 5 4 8 7 
0 1 2 3 9 6 5 4 8 7 
0 1 2 3 4 6 5 9 8 7 
0 1 2 3 4 5 6 9 8 7 
0 1 2 3 4 5 6 9 8 7 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
After sort: 
0 1 2 3 4 5 6 7 8 9 
http://www.lryc.cn/news/253197.html

相关文章:

  • Arduino驱动MLX90614红外测温传感器(温湿度传感器)
  • Ubuntu上传文件到SMB共享文件夹
  • 【Linux】基础IO--重定向理解Linux下一切皆文件缓冲区
  • RINEX介绍
  • ROS-ROS通信机制-服务通信
  • chown和chmod
  • 【GPU】linux 安装、卸载 nvidia 显卡驱动、cuda 的官方文档、推荐方式(runfile)
  • 6页手写笔记总结信号与系统常考知识大题知识点
  • Qt-QSplitter正确设置比例
  • 一篇吃透大厂面试题,2024找工作一帆风顺。
  • 【1day】用友 U8 Cloud系统TaskTreeQuery接口SQL注入漏洞学习
  • 华为快应用中自定义Slider效果
  • C语言每日一题(43)旋转链表
  • CCF计算机软件能力认证考试—202209-1如此编码
  • Ubuntu18.04安装Ipopt-3.12.8流程
  • linux 内核同步互斥技术之信号量
  • 交通强国添力量 无人机巡航为何备受期待?
  • 【PID学习笔记 6 】控制系统的性能指标之二
  • ZLMediakit-method ANNOUNCE failed: 401 Unauthorized(ffmpeg、obs推流rtmp到ZLM发现的问题)
  • 聊聊logback的ThrowableProxyConverter
  • Kubernetes(k8s)访问不了Pod服务
  • python-学生管理|汉罗塔
  • python 堆与栈
  • 园区规划技术要点
  • 深入浅出 Linux 中的 ARM IOMMU SMMU III
  • Linux系统---图书管理中的同步问题
  • Vue学习笔记-activated和deactivated生命周期
  • 102.套接字-Socket网络编程4(TCP通信流程)
  • spring boot 2 升级到 spring boot 3 后文件上传失败
  • Java Stream API 提供了一种非常方便的方式来比较两个 List 的差异,并取出不同的对象