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

熱線電話:13121318867

登錄
首頁精彩閱讀Python實(shí)現(xiàn)堆排序的方法詳解
Python實(shí)現(xiàn)堆排序的方法詳解
2018-06-06
收藏

Python實(shí)現(xiàn)堆排序的方法詳解

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

堆排序作是基本排序方法的一種,類似于合并排序而不像插入排序,它的運(yùn)行時(shí)間為O(nlogn),像插入排序而不像合并排序,它是一種原地排序算法,除了輸入數(shù)組以外只占用常數(shù)個(gè)元素空間。

堆(定義):(二叉)堆數(shù)據(jù)結(jié)構(gòu)是一個(gè)數(shù)組對(duì)象,可以視為一棵完全二叉樹。如果根結(jié)點(diǎn)的值大于(小于)其它所有結(jié)點(diǎn),并且它的左右子樹也滿足這樣的性質(zhì),那么這個(gè)堆就是大(小)根堆。

我們假設(shè)某個(gè)堆由數(shù)組A表示,A[1]為樹的根,給定某個(gè)結(jié)點(diǎn)的下標(biāo)i,其父結(jié)點(diǎn)、左孩子、右孩子的下標(biāo)都可以計(jì)算出來:

PARENT(i):
    return i/2
LEFT(i):
    return 2i
RIGHT(i):
    return 2i+1

堆排序Python實(shí)現(xiàn)

所謂堆排序的過程,就是把一些無序的對(duì)象,逐步建立起一個(gè)堆的過程。
下面是用Python實(shí)現(xiàn)的堆排序的代碼:


defbuild_max_heap(to_build_list):
  """建立一個(gè)堆"""
  # 自底向上建堆
  foriinrange(len(to_build_list)/2-1,-1,-1):
    max_heap(to_build_list,len(to_build_list), i)
defmax_heap(to_adjust_list, heap_size, index):
  """調(diào)整列表中的元素以保證以index為根的堆是一個(gè)最大堆"""
  # 將當(dāng)前結(jié)點(diǎn)與其左右子節(jié)點(diǎn)比較,將較大的結(jié)點(diǎn)與當(dāng)前結(jié)點(diǎn)交換,然后遞歸地調(diào)整子樹
  left_child=2*index+1
  right_child=left_child+1
  ifleft_child < heap_sizeandto_adjust_list[left_child] > to_adjust_list[index]:
    largest=left_child
  else:
    largest=index
  ifright_child < heap_sizeandto_adjust_list[right_child] > to_adjust_list[largest]:
    largest=right_child
  iflargest !=index:
    to_adjust_list[index], to_adjust_list[largest]=\
    to_adjust_list[largest], to_adjust_list[index]
    max_heap(to_adjust_list, heap_size, largest)
defheap_sort(to_sort_list):
  """堆排序"""
  # 先將列表調(diào)整為堆
  build_max_heap(to_sort_list)
  heap_size=len(to_sort_list)
  # 調(diào)整后列表的第一個(gè)元素就是這個(gè)列表中最大的元素,將其與最后一個(gè)元素交換,然后將剩余的列表再調(diào)整為最大堆
  foriinrange(len(to_sort_list)-1,0,-1):
    to_sort_list[i], to_sort_list[0]=to_sort_list[0], to_sort_list[i]
    heap_size-=1
    max_heap(to_sort_list, heap_size,0)
if__name__=='__main__':
  to_sort_list=[4,1,3,2,16,9,10,14,8,7]
  heap_sort(to_sort_list)
  printto_sort_list



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

若不方便掃碼,搜微信號(hào):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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }