[Shuo. Love Python] Heapsort (Heap sort)

Source: Internet
Author: User
Tags shuffle

Def adjust (a, root, n):     k = a[root-1]    c  = root * 2    while c <= n:     &NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;C&NBSP;+&NBSP;1&NBSP;&LT;=&NBSP;N&NBSP;AND&NBSP;A[C]&NBSP;&GT;&NBSP;A[C-1]:             c += 1         if a[c-1] <= k:        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BREAK&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[C/2-1]&NBSP;=&NBSP;A[C-1]         c *= 2    a[c/2-1] =  Kdef heapsort (a):     n = len (a)     for i in  xrange (n / 2, 0, -1):         adjust (a,  I, n) &NBSP;&NBSP;&NBSp; for i in xrange (n - 1, 0, -1):         a[0], a[i] = a[i], a[0]        adjust ( a, 1, i) if __name__ ==  ' __main__ ':    from random  Import shuffle    data = range (+)     shuffle (data)     print data    heapsort (data)     print  data


Liu Shuo teacher Python Boutique Courses:

" Python advanced Programming Skills Combat":

" python algorithm Combat video Course":

" Python scientific Computing-numpy Combat Course": < Strong style= "padding:0px;margin:0px;" >

panda tv Live room:

http://www.panda.tv/671023



[Shuo. Love Python] Heapsort (Heap sort)

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.