The usual sort of algorithm: bubble sort, select sort

Source: Internet
Author: User

Bubble Sort:

Consider the elements to be sorted as "bubbles" that are vertically arranged, smaller elements lighter and thus upward

#!/bin/env python#_*_ coding:utf-8 _*_# algorithm bubble sort li = [13,22,6,99,11]for m in range (Len (LI)-1): if LI[M] > li[m+1]: temp = li[m] li[m] = li[m+1] li[m+1] = temp# for the first time # 13>22->pass# the second time # 22>6->li = [13,6,22,99, All] #第三次 # 22>99->pass# fourth time # 99>11->li = [13,6,22,11,99] #最大的数字已经找到99, and in the end, next only need to compare the first 4 digits for m in range (len (LI)-2): if LI[M] > li[m+1]: temp = li[m] li[m] = li[m+1] li[m+1] = temp# First time # 13>6->li = [6,13,22,11,99] #第二次 # 13>22->pass# Third # 22>11->li = [6,13,11,22,99] #前4个数字最大数找到22, put in the last, then just compare the first 3 digits for M in range (Len (LI)-3): if LI[M] > li[m+1]: temp = li[m] li[m] = li[m+1] li[m+1] = temp# First time # 6&G  T;13->pass# The second time # 13>11->li = [6,11,13,22,99] #前3个数字最大数已找到13, put in the last, then only need to compare the first 2 digits for m in range (Len (LI)-4): If LI[M] > li[m+1]: temp = li[m] li[m] = li[m+1] li[m+1] = temp# First # 6>11->pass# top 2 digits maximum of 11, Put in the last print li#----------------≫ The end result is: li=[6,11,13,22,99]########### #简化版 ############################################################ #list = [ 13,22,6,99,11]for N in range (1,len (list)-1): #n =1,2,3,4 #第一次大循环, n=1,m=4 for M in range (len (list)-N): #第一次小循环m [0 ]>m[1], second m[1]>m[2], third [m2]>[m3], fourth time [M3>M4] if list[m] > list[m+1]: #第二次大循环, n=2,m=3 t EMP = list[m] #第一次小循环m [0]>m[1], second m[1]>m[2], third [m2]>[m3] list[m] = list[m+1] #第三次大循环, n= 3,m=2 list[m+1] = temp #第一次小循环m [0]>m[1], second m[1]>m[2] #第四次大循环,  N=4,m=1 #第一次小循环m [0]>m[1]print list#----------------> Final result: list=[6,11,13,22,99]

  

Select sort

First select the middle value, then put smaller than it on the left, large on the right side (the specific implementation is to find from both sides, find a pair after exchange). Then use the process separately on both sides (recursion)

#!/bin/env python#_*_ coding:utf-8 _*_import timeimport random# production random list Def get_randow (): list = [] for x in range (100) : i = Random.randrange (list.append) (i) Return listdef select_sort (list): For I in range (len list) ): #for I in range (5) #i =0,1,2,3,4 for J in Range (I,len (list)): #for J in range (0,5) #i =0,j=0,1,2,3                   , 4 if list[i] > list[j]: #if 13>13->pass tmp = list[i]                #if 13>22->pass List[i] = list[j] #if 13>6->list=[6,22,13,99,11] LIST[J] = tmp #if 6>99->pass #if 6>                      ->passif __name__ = = ' __main__ ': #for j in range (1,5) #i =1,j=1,2,3,4 list = [13,22,6,99,11] #if 22>22->pass #list = Get_randow () #if 22>13->list=[6,13 , 22,99,11] Start_time = Time.time () #if 13>99->pass select_sort (list) #if 13>11-                                  >LIST=[6,11,22,99,13] End_time = Time.time () #for J in range (2,5) #i =2,j=2,3,4 Print List                                                #if 22>22->pass print ' time:%s '% (end_time-start_time) #if 22>99->pass                                            #if 22>13->list=[6,11,13,99,22]                                                #for j in range (3,5) #i =3,j=3,4 #if 99>99->pass #if 99>22->list=[6,11,13,22,99] # For j in Range (3,5) #i =4,j=4 #if 99>99->pass

  

The usual sort of algorithm: bubble sort, select sort

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.