I wrote this implementation of the median of the median algorithm in python, but it doesn't seem to give the correct result, and it also doesn't seem like linear complexity to me, any idea where I went next?
def select(L): if len(L) < 10: L.sort() return L[int(len(L)/2)] S = [] lIndex = 0 while lIndex+5 < len(L)-1: S.append(L[lIndex:lIndex+5]) lIndex += 5 S.append(L[lIndex:]) Meds = [] for subList in S: print(subList) Meds.append(select(subList)) L2 = select(Meds) L1 = L3 = [] for i in L: if i < L2: L1.append(i) if i > L2: L3.append(i) if len(L) < len(L1): return select(L1) elif len(L) > len(L1) + 1: return select(L3) else: return L2
The function is called like this:
L = list(range(100)) shuffle(L) print(select(L))
LE: Sorry. GetMed was a function that just sorted the list and returned the item to len (list), it should have been selected there, I fixed it now, but still I get the wrong results. Regarding the indentation, the code works without errors, and I see nothing wrong with that: - ??
LE2: I expect 50 (for the current L), it gives me outputs from 30 to 70, no more no less (for now)
LE3: Thank you very much for doing the trick that it is currently working. I'm confused though, I'm trying to make a comparison between this method and the naive one, where I just sort the array and output the results. Now, from what I have read so far, the temporary complexity of the select method should be O (n) Deterministic choice . Although I probably can't compete with python optimization developers, I expected closer results than I got, for example, if I changed the range of the list to 10,000,000, select the output in 84.10837116255952 seconds, while the sort and return method does this at 18.92556029528825. What are some good ways to make this algorithm faster?
python algorithm
cpp_ninja May 29 '12 at 8:32 pm 2012-05-29 20:32
source share