Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python 面试题 - 堆排序 & 演算过程

$
0
0
python 面试题 - 堆排序 & 演算过程

一点号书圈4小时前

热文导读 | 点击标题阅读


php?url=0Fo2QNHlbv" alt="Python 面试题 - 堆排序 & 演算过程" />

今天头给我份Python招聘的笔试题,让我看看难度如何?

最后编程大题是: 请使用python实现整数数组的推排序?

由于过于一直对于此排序很触头,如何用python实现让我有些头疼,于是度娘理清了下概念,开始自己实现,并附上推演过程。

<span style="font-size:14px;"#! /usr/bin/env python

# -*- coding: utf-8 -*-

# vim: tabstop=4 shiftwidth=4 softtabstop=4

# TODO: left child

# param: index

# return: the index of left child

def leftChild(index):

return index*2+1

# TODO: right child

# param: index

# return: the index of right child

def rightChild(index):

return index*2+2

# TODO: max exchange

# param: array index headSize

def maxHeap(array, index, heapSize):

# 01 Get the left and right node

leftInd = leftChild(index)

rightInd = rightChild(index)

# 02 compare the left,right,index vals

# get the max val and ind

largest = index

if leftInd < heapSize and array[index] < array[leftInd]:

largest = leftInd

if rightInd < heapSize and array[leftInd] < array[rightInd]:

largest = rightInd

# 03 exchange the largest and index val when index -ne largest and then recursive

if largest != index:

array[largest], array[index] = array[index], array[largest]

maxHeap(array,largest,heapSize)

# TODO build the heap

# param: array

def buildHeap(array):

for i in range(len(array)/2,-1,-1):

maxHeap(array,i,len(array))

# TODO: heap sort

# param: array

# return: heap sorted array

def heapSort(array):

buildHeap(array)

for i in range(len(array)-1,0,-1):

array[0], array[i] = array[i], array[0]

maxHeap(array,0,i)

arr=[1,2,7,4,34,25,67]

heapSort(arr)

print arr</span

Result: [1, 2, 4, 7, 25, 34, 67]

=============华丽的分割线=================

名词解释:

初始数组: 输入,需要排序的数组

初始堆:基于初始数组,创建符合堆特征的完全二叉树

大根堆头: 大根堆的根节点

无序堆:对排序中取出大根堆头后的剩余堆。

1[0]:1为数组的值,0代表标识位

以下为推演过程:

A. 初始数组: 1[0] 2[1] 7[2] 4[3] 34[4] 25[5] 67[6]

B. 初始堆:

子条件区间:

1. 条件:index = len(arr)/2 值为3

leftChild: 3*2+1=7 rightChild: 3*2+2=8 此时都大于arrSize(heapSize) Pass

2. 条件: 2

leftChild: 2*2+1=5 rightChild: 2*2+2=6 此时最大值67[6],与7[2] 交换。 此时数组变为:1[0] 2[1] 67[2] 4[3] 34[4] 25[5] 7[6]

2.1 子条件: 6

leftChild: 6*2+1=13 rightChild: 6*2+2=14 此时都大于arrSize(heapSize) 递归回归

3. 条件: 1

leftChild: 1*2+1=3 rightChild: 1*2+2=4 此时最大值34[4],与2[1]交换 此时数组变为:1[0] 34[1] 67[2] 4[3] 2[4] 25[5] 7[6]

3.1 子条件:4

leftChild: 4*2+1=9 rightChild: 4*2+2=10 此时都大于arrSize(heapSize) 递归回归

4. 条件:0

leftChild: 0*2+1=1 rightChild: 0*2+2=2 此时最大值67[2],与1[0]交换 此时数组变为: 67[0] 34[1] 1[2] 4[3] 2[4] 25[5] 7[6]

4..1 子条件: 2

leftChild: 2*2+1=5 rightChild: 2*2+2=6 此时最大值25[5], 与1[2]交换 此时数组变为: 67[0] 34[1] 25[2] 4[3] 2[4] 1[5] 7[6]

4.2 子条件:5

leftChild: 5*2+1=11 rightChild: 5*2+2=12 此时都大于arrSize(heapSize) 递归回归

初始堆为:67[0] 34[1] 25[2] 4[3] 2[4] 1[5] 7[6]

树形展示为:

67[0]
/ \
34[1] 25[2]
/ \ / \
4[3] 2[4] 1[5] 7[6]

C 堆排序:

注意: 条件一直是无序堆的0

1. 交换大堆根头和最后的一个元素。

交换67[0]与7[6],并将67[0]从无序堆中取出,此时无序堆: 7[0] 34[1] 25[2] 4[3] 2[4] 1[5] 根据条件 0 来整理无序堆(逻辑同上): 34[0] 7[1] 25[2] 4[3] 2[4] 1[5] 2. 交换34[0]与1[5],并将34[0]从无序堆中取出,此时无序堆: 1[0] 7[1] 25[2] 4[3] 2[4] 根据条件 0 来整理无序堆为:25[0] 7[1] 1[2] 4[3] 2[4]

3. 重复1和2

最后无序堆中的元素全部取出,并组成堆排序的最后结果。

[1, 2, 4, 7, 25, 34, 67]

作者 | 图文来自网络、如涉及版权问题,请联系我们以便处理。文章内容纯属作者个人观点,不代表本网观点。

读书吧| QQ群:543839145

-END-

----后台回复对应字母,获取相关精彩内容----

【C1】最新教育、大数据、编程、科技文章和资料

【C2】往期公众号精彩文章

【C3】教学视频、直播、教学论坛回顾

【C4】计算机类推荐教材


Viewing all articles
Browse latest Browse all 9596

Trending Articles