From 91dfb040f7a189af68e8eefd459f6cc13eb9b7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nd=C3=A8ye=20A=C3=AFssatou=20GASSAMA?= Date: Tue, 19 Apr 2022 16:13:34 +0000 Subject: [PATCH] Add n_th minimum --- algorithms/Python/README.md | 1 + algorithms/Python/recursion/nth_minimum.py | 30 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 algorithms/Python/recursion/nth_minimum.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 4e132715..dcb0bf8b 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -23,6 +23,7 @@ ## Recursion - [Factorial](recursion/factorial.py) - [n-th Fibonacci number](recursion/nth_fibonacci_number.py) +- [n-th minimum of a list](recursion/nth_minimum.py) - [Recursive Insertion Sort](recursion/recursive_insertion_sort.py) ## Scheduling diff --git a/algorithms/Python/recursion/nth_minimum.py b/algorithms/Python/recursion/nth_minimum.py new file mode 100644 index 00000000..47406704 --- /dev/null +++ b/algorithms/Python/recursion/nth_minimum.py @@ -0,0 +1,30 @@ +# Returns the nth smallest value in an unordered list +# The idea is the following: +# 1. Choose a pivot value (here the middle element) +# 2. Split the list in 3 parts. The first part contains all the elements smaller than the pivot value, the second part contains all the elements equal to the pivot value and the third part contains all the elements greater than the pivot value. +# 3. If n is smaller than the length of the first part, then the nth smallest element is in the first part. Do the same for the second part and third part. +def nth_min(arr, n): + if n > len(arr) or n < 1: + return None + v = arr[len(arr)//2] # get the middle element + arr_left = [] + arr_right = [] + arr_v = [] + for i in range(len(arr)): + if arr[i] < v: + arr_left.append(arr[i]) + elif arr[i] > v: + arr_right.append(arr[i]) + else: + arr_v.append(arr[i]) + if n <= len(arr_left): + return nth_min(arr_left, n) + elif n > len(arr_left) and n <= len(arr_left) + len(arr_v): + return arr_v[0] + else: + return nth_min(arr_right, n - len(arr_left) - len(arr_v)) + + +l = [5, 6, 4, 8, 3, 7, 2, 1, 9] +print(nth_min(l, 12)) +print(nth_min(l, 5))