python 實(shí)現(xiàn)在無(wú)序數(shù)組中找到中位數(shù)方法
一、問(wèn)題描述
1、求一個(gè)無(wú)序數(shù)組的中位數(shù), (若數(shù)組是偶數(shù),則中位數(shù)是指中間兩個(gè)數(shù)字之和除以2,若數(shù)組是奇數(shù),則中位數(shù)是指最中間位置。要求:不能使用排序,時(shí)間復(fù)雜度盡量低
2、例如:
lists = [3, 2, 1, 4] , 中位數(shù)為 = (2+3)/2 = 2.5lists = [3, 1, 2] , 中位數(shù)為 2
3、算法思想:
利用快速排序思想(但是并不是全部使用):任意挑選一個(gè)元素,以該元素為key, 劃分?jǐn)?shù)組為兩個(gè)部分,如果左側(cè)數(shù)組長(zhǎng)度剛好為(n-1)/2, 那么key就為中位數(shù), 若左側(cè)數(shù)組長(zhǎng)度 < (n-1)/2 , 那么中位數(shù)點(diǎn)在右側(cè),反之,中位數(shù)在左側(cè)。然后進(jìn)入相應(yīng)的一側(cè)繼續(xù)尋找中位
平均時(shí)間復(fù)雜度為O(n)
二、程序
class Solution(object): def findmedian(self, lists): if not lists or len(lists) == 0: return [] n = len(lists) if n % 2 == 0: a = self.partition(lists, n/2, 0, n-1) b = self.partition(lists, n/2-1, 0, n-1) mid = (lists[a]+lists[b])/ (2 * 1.0) return mid else: mid = self.partition(lists, n/2, 0, n-1) return lists[mid] def partition(self, lists, k, start, end): key = lists[start] left, right = start, end while left < right: while left < right and lists[right] > key: right = right - 1 lists[left] = lists[right] while left < right and lists[left] < key: left = left + 1 lists[right] = lists[left] lists[left] = key if left == k: return left elif left > k: return self.partition(lists, k, start, left-1) else: return self.partition(lists, k, left+1, end) if __name__ == '__main__': sol = Solution() lists = [2, 5, 4, 9, 3, 6, 8, 7, 1] # lists = [1, 2] data = sol.findmedian(lists) print('中位數(shù) = %s' % data)
知識(shí)補(bǔ)充:python streaming 實(shí)現(xiàn)某個(gè)字段排序
一,hadoop streaming默認(rèn)情況
1,在hadoop streaming的默認(rèn)情況下,是以t作為分隔符的,標(biāo)準(zhǔn)輸入時(shí),每行的第一個(gè)t之前的內(nèi)容作為key,第一個(gè)t之后的內(nèi)容作為value。注意,如果一個(gè)t字符都沒(méi)有,那么整行作為key。
2,streaming的一些參數(shù)如下:
-D stream.map.output.field.separator :設(shè)置map輸出中key和value的分隔符 -D stream.num.map.output.key.fields : 設(shè)置map程序分隔符的位置,該位置之前的部分作為key,之后的部分作為value -D map.output.key.field.separator : 設(shè)置map輸出中key內(nèi)部的分割符-D num.key.fields.for.partition : 指定分桶時(shí),key按照分隔符切割后,其中用于分桶key所占的列數(shù)(配合-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner 使用)-D stream.reduce.output.field.separator:設(shè)置reduce輸出中key和value的分隔符 -D stream.num.reduce.output.key.fields:設(shè)置reduce程序分隔符的位置
二,python streaming 實(shí)現(xiàn)某個(gè)字段的排序
1, 輸入數(shù)據(jù): cat data.txt (中間是tab鍵)
11 211 311 4 111 1
11 12 22
2,streaming程序如下:
vim sorted.sh
#!/bin/bashexport CURRENT=/home/chunhe.liao/hadoop_streaming/sort/usr/local/hadoop-2.6.3/bin/hadoop jar /usr/local/hadoop-2.6.3/share/hadoop/tools/lib/hadoop-streaming-2.6.3.jar -D stream.map.output.field.separator=’t’ -D stream.num.map.output.key.fields=3 -D mapreduce.job.output.key.comparator.class=org.apache.hadoop.mapreduce.lib.partition.KeyFieldBasedComparator -D mapreduce.partition.keycomparator.options=-k3,3nr # 按照第三列逆序排列,可以根據(jù)想要的第幾段來(lái)選擇。-input '/user/test/inputdata/datas3/data.txt' -output '/user/test/streaming/sorted_20180711' -mapper 'python mapper.py' -reducer 'python reducer.py' -file '$CURRENT/mapper.py' -file '$CURRENT/reducer.py'
(2) mapper.py
# -*- coding: utf-8 -*-import sys for line in sys.stdin: line = line.strip() print(’{0}’.format(line))
(3) reducer.py
# -*- coding: utf-8 -*-import sys for line in sys.stdin: line = line.strip() print('{0}'.format(line))
運(yùn)行命令:
bash sorted.sh
運(yùn)行結(jié)果:
hdfs dfs -cat /user/test/streaming/sorted_20180711/part-00000
11 12 2211 311 211 4 111 1
以上這篇python 實(shí)現(xiàn)在無(wú)序數(shù)組中找到中位數(shù)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解2. 淺談python出錯(cuò)時(shí)traceback的解讀3. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼4. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解5. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Nginx+php配置文件及原理解析8. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法9. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析
