Python sorting algorithm

Source: Internet
Author: User

Do not feel that there have been six months have not written, time is really easy to neglect, this half a year after a spring festival, to Lhasa Travel. The job is also very busy, did not carry out systematic study and summary.

This year began to calm down to start learning from the basic, mainly divided into three parts, algorithm, linear algebra, probability statistics.

The first learning algorithm, in Python language implementation, mainly in order to understand the idea, after all, there are many wheels off the shelf. The next study of linear algebra and probability statistics, mainly through the two books in Japan, the implementation of programming from the numerical analysis began to learn, perhaps to use Matlab.

Algorithm, learning the book is "Python algorithm tutorial", covering the knowledge of the whole, the shortcomings of the need to have a certain foundation, I read the book first, and read the "Programmer's Math" (this looks too easy), and then the inside of the idea has a perceptual understanding, but specific to the algorithm, or it is difficult to write out, So want to put the book involved in the problem, code, all to achieve their own, as a review.

Starting with the simplest sort, you can start from scratch to the end, or in turn, you can write recursion or write loops.

1. From the beginning to the end, is to choose the small, put to the front.

Recursive

1 defSort_f (seq,i):2     ifi==0:3         return4Sort_f (seq,i-1)5j=I6      whileJ>0 andSeq[j-1]>Seq[j]:7Seq[j],seq[j-1]=seq[j-1],seq[j]8J-=19 Tenseq = [9,8,7,6,5,4,3,2,1] One  A------------------------------------------------------- -  ->>> sort_f (Seq,len (seq)-1) the>>>seq -[1, 2, 3, 4, 5, 6, 7, 8, 9]

Cycle

def sort_f2 (seq):      for inch range (len (seq)):          for inch Range (I,len (seq)):             if seq[j]<seq[i]:                seq[i],seq[j]=seq[j],seq[i]

2. From the end of the tail, is to choose the largest, put in the last

Recursive

def sort_e (seq,i):     if i==0:        return    sort_e (seq,i-1)    = I      while  and Seq[j]<seq[j-1]:        seq[j],seq[j-1]=seq[j-1],seq[j]        J-=1

Recursive or write like this

def Sort_ee (seq,i):     if i==0:        return    maxj=i    for in Range (i):         if seq[j]>SEQ[MAXJ]:            maxj=J    Seq[i],seq[maxj]=seq[maxj],seq[i ]    Sort_ee (seq,i-1)

Cycle

def sort_e2 (seq):    i=len (seq)-1     while i>0:        for in  Range (i):            if seq[j]>  Seq[i]:                seq[i],seq[j]=Seq[j],seq[i]        i-=1

Python sorting algorithm

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.