Python Select sort Simple implementation

Source: Internet
Author: User

#!/usr/bin/env python3# -*- coding: UTF-8 -*-# Date:2018-5-27# Author:AreLIN#选择排序:每次只取一个最大或最小值的索引import randoma = []count_swap = 0count_iter = 0#随机数列表for _ in range(20): a.append(random.randrange(1,101))print("my first list:\n{}".format(a))for i in range(len(a)): maxindex = i for j in range(i+1,len(a)): count_iter += 1 if a[maxindex] < a[j]: maxindex = j if i != maxindex: a[maxindex],a[i] = a[i],a[maxindex] count_swap += 1print("My end list:\n{} {} {}".format(a,count_iter,count_swap))
#二元选择排序:一次取两个值(最大最小)的索引a = []count_swap = 0count_iter = 0#随机数列表for _ in range(20):    a.append(random.randrange(1,101))print("my first list:\n{}".format(a))for i in range(len(a)//2):    maxindex = i    minindex = -i - 1    minorign = minindex    for j in range(i+1,len(a)-i):        count_iter += 1        if a[maxindex] < a[j]:            maxindex = j        if a[minindex] > a[-j-1]:            minindex = -j - 1    print("{} {} ".format(maxindex,minindex),end=‘|‘)    if i != maxindex:        a[maxindex],a[i] = a[i],a[maxindex]        count_swap += 1        if (i == minindex) or (i == (len(a) + minindex)):            minindex = maxindex    if minorign != minindex:        a[minindex],a[minorign] = a[minorign],a[minindex]        count_swap += 1print("The end list is:\n{},count_swap:{},count_iter:{}".format(a,count_swap,count_iter))

Binary selection sequencing loops are less and more efficient

Python Select sort Simple implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.