diff --git a/book/book_second_edition.pdf b/book/book_second_edition.pdf index 82b0f38..245e104 100644 Binary files a/book/book_second_edition.pdf and b/book/book_second_edition.pdf differ diff --git a/src/searching_and_sorting/searching/quick_select.py b/src/searching_and_sorting/searching/quick_select.py new file mode 100644 index 0000000..1abcb92 --- /dev/null +++ b/src/searching_and_sorting/searching/quick_select.py @@ -0,0 +1,41 @@ + +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + +def quickSelect(seq, k): + # this part is the same as quick sort + len_seq = len(seq) + if len_seq < 2: return seq + + ipivot = len_seq // 2 + pivot = seq[ipivot] + + smallerList = [x for i,x in enumerate(seq) if x <= pivot and i != ipivot] + largerList = [x for i,x in enumerate(seq) if x > pivot and i != ipivot] + + # here starts the different part + m = len(smallerList) + if k == m: + return pivot + elif k < m: + return quickSelect(smallerList, k) + else: + return quickSelect(largerList, k-m) + + + + +if __name__ == '__main__': + # Checking the Answer + seq = [10, 60, 100, 50, 60, 75, 31, 50, 30, 20, 120, 170, 200] + + # we want the middle element + k = len(seq) // 2 + + # Note that this only work for odd arrays, since median in + # even arrays is the mean of the two middle elements + print(quickSelect(seq, k)) + import numpy + print numpy.median(seq) \ No newline at end of file