The way of the Python-day 6-three basic sort, recursion, reflection, module

Source: Internet
Author: User
Tags for in range

1. Three ways to sort

-Bubble Sort: 22 comparison, big row to the right, every time a bubble pops out

m = [1,2,6,5,4] for in  range (len (m)):    is in range (i):         if m[j] > m[j+1]:            m[j],m[j+1] = m[j+1],m[j]print(m)

-Select sort

  for a set of keywords {k1,k2,..., Kn}, first from K1,K2 Select the minimum value,..., kn, and if it is a KZ, swap the KZ with K1;  
then select the minimum KZ from K2,k3, ..., kn, and then swap the KZ with the K2.
so choose and Exchange N-2, section (n-1), Select the minimum value from Kn-1, kn the KZ will swap the KZ with Kn-1, and the last is the maximum value in the sequence, and
a small to large ordered sequence is formed.

m = [1,4,6,2,5] for in range (len (m)):    = i    -in  Range (I,len (m)):        if m[min]> m[j]:            = J    = m[ Min],m[i]print(m)

-Insert Sort

  Generally, the insertion sort is implemented by in-place on the array. The specific algorithm is described as follows:  
1. Starting with the first element, The element can be thought to have been sorted
2. Remove the next element, and then scan from backward forward in the sequence of elements that are already sorted
3. If the element (sorted) is greater than the new element, move the element to the next position
4. Repeat step 3 until you find that the sorted element is less than or equal to the new element's position
5. After inserting a new element into the location

m = [1,4,6,2,5] for in range (1, Len (m)):    = M[i]    = I      while  and m[j-1]>Save:        = m[j-1]        J-=1    = Save Print (m)

-Attached: A method for taking a set of random numbers

# http://blog.csdn.net/hinyunsin/article/details/6311707 Import Random def getrandata (num):     = []    = 0    while i <num:        a.append (random.randint (0,1000000 ) )        +=1    return= getrandata (5)print(n)

2. Recursion

# 1*2*3*4*5*6*7 def func (num):     if num = =1        :return 1    return num * func (num-1 = func (7) 
    Print(x)print(1*2*3*4*5*6*7)

3. Reflection

Reflection is a string-based way to manipulate its members in a module, mainly the attr series. Its purpose is to simplify the code, directly based on the contents of input to the module to find functions

The way of the Python-day 6-three basic sort, recursion, reflection, module

Related Article

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.