python选择排序算法实例总结(精选4篇)
这篇文章主要介绍了python选择排序算法,以三个实例以不同方法分析了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 positionif 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
代码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
-10-10python笔记(1) 关于我们应不应该继续学习python
2014-01-01python文件比较示例分享
-11-11python中文乱码的解决方法
2014-06-06测试、预发布后用python检测网页是否有日常链接
2014-02-02Python操作列表的常用方法分享
2014-06-06用python登录Dr.com思路以及代码分享
2013-11-11pyramid配置session的方法教程
2014-01-01python学习手册中的python多态示例代码
电脑资料
我们知道有序数组对于快速算法是致命的,如果不对快速算法做任何优化,那么快速算法将会达到最差运行时间
快速选择算法里面也是一样的,应该避免输入有序数组导致的分组极度不平衡的情况,所以我们就做了下面的优化,在进行快速选择之前,首先从数组的头部,中部,尾部选出三个元素出来,找出这三个元素中第二大的元素,并与数组的最后一个元素进行交换,这样我们就可以避免分组极度不平衡的情况了,但是只是能保证避免分组极度不平衡的情况,还是有可能分组不平衡的,下面我们要讲解的BFPRT选择算法
可以很好的做到平衡分组
我们在每次迭代或者递归进行之前加上下面的代码
/** 省略了好多代码 */if(leftBorder >rightBorder) return ; // 这里采用快速排序的思想来完成 // 为了避免最差的情况发生 int mid=(leftBorder+rightBorder)/2; int midPos = rightBorder; // mid元素是第二大的 if((array[leftBorder] >array[mid] && array[mid] >array[rightBorder]) || (array[leftBorder] < array[mid] && array[mid] < array[rightBorder]) ) midPos = mid; // left元素是第二大的 else if((array[mid] >array[leftBorder] && array[leftBorder] >array[rightBorder]) ||(array[mid] < array[leftBorder] && array[leftBorder] < array[rightBorder]) ) midPos = leftBorder; if(midPos != rightBorder) exchange(array , midPos , rightBorder); int i = leftBorder-1; int j = leftBorder; /** 省略了好多代码 */
BFPRT选择算法
上面我们讲过,在快速排序算法里面如果主元选择的不合适,将会导致快速排序算法的分组极度不平衡,这样大大降低了快速选择算法的效率
代码如下:
package main
import “fmt”
func select_sort(a []int) {
len := len(a)
for i:=0; i < len-1; i++ {
k := i
j:= i + 1
for ; j < len; j++ {
if a[j] < a[k] { k = j }
}
if k != i {
a[i], a[k] = a[k], a[i]
}
}
}
func print_array(a []int) {
for i := 0; i < len(a) - 1; i++ {
fmt.Printf(“%d, ”, a[i])
}
fmt.Print(a[len(a)-1])
}
func main() {
a := []int{1, 8, 5, 9, 4, 3, 6, 6}
print_array(a)
fmt.Printf(“ ”)
select_sort(a)
print_array(a)
}
输入:
1, 8, 5, 9, 4, 3, 6, 6
输出:
1, 3, 4, 5, 6, 6, 8, 9
【python选择排序算法实例总结】推荐阅读:
python是什么语言11-19
python人工智能教学06-21
Python中的hypot方法使用简介11-19
Python读写unicode文件的方法02-26
塑料模具材料选择实例01-09
python获取一组汉字拼音首字母的方法02-07
传感器选择总结03-28
无悔的选择工作总结07-08
选择青春,选择无悔12-09