99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python實現(xiàn)的堆排序算法原理與用法實例分析
Python實現(xiàn)的堆排序算法原理與用法實例分析
2018-04-14
收藏

Python實現(xiàn)的堆排序算法原理與用法實例分析

本文實例講述了Python實現(xiàn)的堆排序算法。分享給大家供大家參考,具體如下:

堆排序(Heapsort)是指利用堆這種數(shù)據(jù)結(jié)構(gòu)所設(shè)計的一種排序算法。堆是一個近似完全二叉樹的結(jié)構(gòu),并同時滿足堆性質(zhì):即子結(jié)點的鍵值或索引總是小于(或者大于)它的父節(jié)點。
具體代碼如下:    
#-*- coding: UTF-8 -*-
import numpy as np
def MakeHeap(a):
  for i in xrange(a.size / 2 - 1, -1, -1):#對非葉子節(jié)點的子節(jié)點進行調(diào)節(jié),構(gòu)建堆
    AdjustHeap(a, i, a.size)
def AdjustHeap(a, i, n):
  j = i*2 +1                     #選擇節(jié)點i的左子節(jié)點
  x = a[i]                       #選擇節(jié)點的數(shù)值
  while j < n:                    #循環(huán)對子節(jié)點及其子樹進行調(diào)整
    if j + 1 < n and a[j+1] < a[j]:    #找到節(jié)點i子節(jié)點的最小值
      j += 1
    if a[j] >= x :                  #若兩個子節(jié)點均不小于該節(jié)點,則不同調(diào)整
      break
    a[i], a[j] = a[j], a[i]             #將節(jié)點i的數(shù)值與其子節(jié)點中最小者的數(shù)值進行對調(diào)
    i = j                        #將i賦為改變的子節(jié)點的索引
    j = i*2 + 1                   #將j賦為節(jié)點對應的左子節(jié)點
def HeapSort(a):
  MakeHeap(a)                 #構(gòu)建小頂堆
  for i in xrange(a.size - 1,0, -1):   #對堆中的元素逆向遍歷
    a[i], a[0] = a[0], a[i]           #將堆頂元素與堆中最后一個元素進行對調(diào),因為小頂堆中堆頂元素永遠最小,因此,輸出即為最小元素
    AdjustHeap(a, 0, i)          #重新調(diào)整使剩下的元素仍為一個堆
if __name__ == '__main__':
  a = np.random.randint(0, 10, size = 10)
  print "Before sorting..."
  print "---------------------------------------------------------------"
  print a
  print "---------------------------------------------------------------"
  HeapSort(a)
  print "After sorting..."
  print "---------------------------------------------------------------"
  print a[::-1]                    #因為堆排序按大到小進行排列,采用a[::-1]對其按從小到大進行輸出
  print "---------------------------------------------------------------"

運行結(jié)果:


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }