博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python选择排序_Python选择排序
阅读量:2506 次
发布时间:2019-05-11

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

python选择排序

Here you’ll learn about python selection sort algorithm with program example.

在这里,您将通过程序示例了解python选择排序算法。

Selection sort is one of the easiest sorting algorithm out there. In Selection sort, to sort an unsorted array, all we have to do is find the minimum in the array and swap it with the first element in the unsorted array. After each swapping increase the starting index of the array by one.

选择排序是最简单的排序算法之一。 在选择排序中,要对未排序的数组进行排序,我们要做的就是在数组中找到最小值,然后将其与未排序的数组中的第一个元素交换。 每次交换之后,将数组的起始索引增加一个。

This is for sorting in ascending order. If we want to sort it into descending order find the maximum instead of minimum and replace with the first element in unsorted array.

这是按升序排序的。 如果要按降序排序,请找到最大值而不是最小值,并替换为未排序数组中的第一个元素。

Python选择排序 (Python Selection Sort)

(Example)

We have an unsorted array-

我们有一个未排序的数组-

[4,8,19,2,28,21]

[4,8,19,2,28,21]

Step 1:

第1步:

Python Selection Sort 1

Step 2:

第2步:

Python Selection Sort 2

Step 3:

第三步:

Python Selection Sort 3

Step 4:

第4步:

Python Selection Sort 4

Step 5:

步骤5:

Python Selection Sort 5

Step 6:

步骤6:

Python Selection Sort 6

算法 (Algorithm)

If we have an array of n elements.

如果我们有n个元素的数组。

Step 1:- set MIN = 0

步骤1:-设定MIN = 0

Step 2:- find minimum element in array

步骤2:-在数组中找到最小元素

If exists then swap with element at MIN

如果存在,则与MIN处的元素交换

Step 3:- increase MIN by 1

步骤3:-将MIN加1

Step 4:- go to Step 2 until  array is sorted (for n-1 times)

步骤4:-转到步骤2直到对数组排序(n-1次)

时间复杂度 (Time Complexity)

  • Best Case = O(n)2

    最佳情况= O(n)2

  • Average Case = O(n)2

    平均情况= O(n)2

  • Worst Case = O(n)2

    最坏情况= O(n)2

Note: for larger arrays we never use selection sort.

注意:对于更大的数组,我们从不使用选择排序。

Python选择排序程序 (Python Selection Sort Program)

Below is the implementation of selection sort in python.

以下是python中选择排序的实现。

arr =[4,8,19,2,28,21] min = 0    #set min = 0n = len(arr) while(min <= n-1):  s_i = min  while(s_i <= n-1):      #finding minimum    if (arr[s_i] < arr[min]):      arr[min],arr[s_i] = arr[s_i],arr[min]   #swapping element at start index with minimum    s_i = s_i+1   min = min+1 for element in arr:  	print element

Output

输出量

2 4 8 19 21 28

2 4 8 19 21 28

Comment below if you have any queries related to above python selection sort tutorial.

如果您对上面的python选择排序教程有任何疑问,请在下面评论。

翻译自:

python选择排序

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

你可能感兴趣的文章
OpenStack的容器服务体验
查看>>
【BZOJ 4059】 (分治暴力|扫描线+线段树)
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>
linux 内核的优化
查看>>
Spark笔记之DataFrameNaFunctions
查看>>
Oracle 时间函数 (转)
查看>>
近端梯度算法(Proximal Gradient Descent)
查看>>
DRM-内容数据版权加密保护技术学习(中):License预发放实现 (转)
查看>>
TCP与UDP协议
查看>>
php上传文件如何保证上传文件不被改变或者乱码
查看>>
目录遍历代码
查看>>
iOS MD5加密实现方法
查看>>
页面中调用系统常用的对话框需要用到的classid
查看>>
cygwin下的目录软连接
查看>>
eclipse控制台不显示输出的解决办法
查看>>
Java中的TCP/UDP网络通信编程
查看>>
Trie树
查看>>