python选择排序算法实例总结
作者:pythoner 发布时间:2023-08-29 06:58:28
标签:python,排序
本文实例总结了python选择排序算法。分享给大家供大家参考。具体如下:
代码1:
def ssort(V):
#V is the list to be sorted
j = 0
#j is the "current" ordered position, starting with the first one in the list
while j != len(V):
#this is the replacing that ends when it reaches the end of the list
for i in range(j, len(V)):
#here it replaces the minor value that it finds with j position
if V[i] < V[j]:
#but it does it for every value minor than position j
V[j],V[i] = V[i],V[j]
j = j+1
#and here's the addiction that limits the verification to only the next values
return V
代码2:
def selection_sort(list):
l=list[:]
# create a copy of the list
sorted=[]
# this new list will hold the results
while len(l):
# while there are elements to sort...
lowest=l[0]
# create a variable to identify lowest
for x in l:
# and check every item in the list...
if x<lowest:
# to see if it might be lower.
lowest=x
sorted.append(lowest)
# add the lowest one to the new list
l.remove(lowest)
# and delete it from the old one
return sorted
代码3
a=input("Enter the length of the list :")
# too ask the user length of the list
l=[]
# take a emty list
for g in range (a):
# for append the values from user
b=input("Enter the element :")
# to ask the user to give list values
l.append(b)
# to append a values in a empty list l
print "The given eliments list is",l
for i in range (len(l)):
# to repeat the loop take length of l
index=i
# to store the values i in string index
num=l[i]
# to take first value in list and store in num
for j in range(i+1,len(l)):
# to find out the small value in a list read all values
if num>l[j]:
# to compare two values which store in num and list
index=j
# to store the small value of the loop j in index
num=l[j]
# to store small charecter are value in num
tem=l[i]
# to swap the list take the temparary list stor list vlaues
l[i]=l[index]
# to take first value as another
l[index]=tem
print "After the swping the list by selection sort is",l
希望本文所述对大家的Python程序设计有所帮助。


猜你喜欢
- 本文代码将一些简单常用的SQL语句,拆分、封装成链式函数与终结函数,链式操作没有先后之分,实现傻瓜式mysql数据库操作。 同时学习下静态成
- 简介在Python开发和测试过程中主要有两种模式可以选择:脚本模式、命令行模式。在代码的开发和调试过程中使用脚本模式很方便,目前比较主流的命
- 先来看个实例#!/usr/bin/env python import sys def search2(a,m):
- $server->connections//server−>connections遍历所有websocket连接用户的fd,给所
- 1.数组的索引我用的是iloc函数。导入数据是data,索引data.iloc[i,j],i代表行,j代表列。如果要索引i行之后的所有行元素
- Python中的缩进(Indentation)决定了代码的作用域范围。这一点和传统的c/c++有很大的不同(传统的c/c++使用花括号{}符
- MySQL安装说明MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle旗下产品。MySQL 是最流行
- 一、安装mysqlclient网上看到很过通过命令:pip install mysqlclient 进行安装的教程,但是我却始终安装失败,遇
- python中join和格式化的用法join用法1、将字符串转化成列表str1 = "hello"print(list(
- 最近看到网上的一些作品,然后进行一些完善。只是用于学习,不要去干坏事哦。程序来源于网,我只是做了一些优化。当然这种方法破解还是有点慢哦。我用
- 分析SQL执行带来的开销是优化SQL的重要手段。在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析。该参数可以在全局和
- 上一篇文章写到原生js取代jquery的一些常用函数:原生js仿jquery一些常用方法,那么,ajax如何实现呢?如下是一个比较完整的aj
- 之前只是单纯的会用,因为vue关于父子组件通讯差别有一点点大。1.在父组件内传递变量的时候,需要加冒号:,否则你就只是单纯的传递了一个字符串
- !DOCTYPE--------------------------------------------------------------
- ImageField的使用笔记今天完善作业写的订单系统,主要是给每一个菜品增加图片,看起来美观一些,但是没想到这个小小的需求花了我一天时间,
- 一、创建虚拟环境python -m venv env通过执行命令,创建一个名为env的虚拟环境,命令执行完毕后会出现一个env文件夹,这是一
- 前言前面我们已经介绍了 python面向对象入门教程之从代码复用开始(一) ,这篇文章主要介绍的是关于Python面向对象之设置对
- 如何快速的求出1到x的和呢?代码如下:NB(注意): # 后面的部分表示输出结果。class Debug: def calcul
- 有空余的时候自己写了一下,代码没有进行很好的规整。如果发现bug请及时通告我,谢谢 主要功能:1、点击插入表情,可选
- 本文记录了mysql 8.0.14 安装配置的过程,供大家参考,具体内容如下1.下载地址:下载地址找到zip压缩文件.2.配置环境变量把解压