- Notifications
You must be signed in to change notification settings - Fork185
Adding the Median algorithm Python Code#422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
JVBravoo wants to merge1 commit intometafy-social:masterChoose a base branch fromJVBravoo:branch-bravoo
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletionsscripts/Median_algorithm/Median.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Median Algorithm | ||
| ###### It brings the median value of an array | ||
| ## Run Script | ||
| The script already has the array hardcoded. | ||
| ## Command to run the script | ||
| python3 Median_Algorithm.py |
135 changes: 135 additions & 0 deletionsscripts/Median_algorithm/Median_Algorithm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import random | ||
| def pick_pivot(l): | ||
| """ | ||
| Pick a good pivot within l, a list of numbers | ||
| This algorithm runs in O(n) time. | ||
| """ | ||
| assert len(l) > 0 | ||
| # If there are < 5 items, just return the median | ||
| if len(l) < 5: | ||
| # In this case, we fall back on the first median function we wrote. | ||
| # Since we only run this on a list of 5 or fewer items, it doesn't | ||
| # depend on the length of the input and can be considered constant | ||
| # time. | ||
| return nlogn_median(l) | ||
| # First, we'll split `l` into groups of 5 items. O(n) | ||
| chunks = chunked(l, 5) | ||
| # For simplicity, we can drop any chunks that aren't full. O(n) | ||
| full_chunks = [chunk for chunk in chunks if len(chunk) == 5] | ||
| # Next, we sort each chunk. Each group is a fixed length, so each sort | ||
| # takes constant time. Since we have n/5 chunks, this operation | ||
| # is also O(n) | ||
| sorted_groups = [sorted(chunk) for chunk in full_chunks] | ||
| # The median of each chunk is at index 2 | ||
| medians = [chunk[2] for chunk in sorted_groups] | ||
| # It's a bit circular, but I'm about to prove that finding | ||
| # the median of a list can be done in provably O(n). | ||
| # Finding the median of a list of length n/5 is a subproblem of size n/5 | ||
| # and this recursive call will be accounted for in our analysis. | ||
| # We pass pick_pivot, our current function, as the pivot builder to | ||
| # quickselect. O(n) | ||
| median_of_medians = quickselect_median(medians, pick_pivot) | ||
| return median_of_medians | ||
| def chunked(l, chunk_size): | ||
| """Split list `l` it to chunks of `chunk_size` elements.""" | ||
| return [l[i:i + chunk_size] for i in range(0, len(l), chunk_size)] | ||
| def nlogn_median(l): | ||
| l = sorted(l) | ||
| if len(l) % 2 == 1: | ||
| return l[int((len(l) / 2))] | ||
| else: | ||
| return 0.5 * (l[len(l) / 2 - 1] + l[len(l) / 2]) | ||
| def quickselect_median(l, pivot_fn=random.choice): | ||
| if len(l) % 2 == 1: | ||
| return quickselect(l, len(l) / 2, pivot_fn) | ||
| else: | ||
| return 0.5 * (quickselect(l, len(l) / 2 - 1, pivot_fn) + | ||
| quickselect(l, len(l) / 2, pivot_fn)) | ||
| def quickselect(l, k, pivot_fn): | ||
| """ | ||
| Select the kth element in l (0 based) | ||
| :param l: List of numerics | ||
| :param k: Index | ||
| :param pivot_fn: Function to choose a pivot, defaults to random.choice | ||
| :return: The kth element of l | ||
| """ | ||
| if len(l) == 1: | ||
| assert k == 0 | ||
| return l[0] | ||
| pivot = pivot_fn(l) | ||
| lows = [el for el in l if el < pivot] | ||
| highs = [el for el in l if el > pivot] | ||
| pivots = [el for el in l if el == pivot] | ||
| if k < len(lows): | ||
| return quickselect(lows, k, pivot_fn) | ||
| elif k < len(lows) + len(pivots): | ||
| # We got lucky and guessed the median | ||
| return pivots[0] | ||
| else: | ||
| return quickselect(highs, k - len(lows) - len(pivots), pivot_fn) | ||
| l = [9,1,0,2,3,4,6,8,7,10,5] | ||
| print(pick_pivot(l)) | ||
| """ | ||
| Considere a lista abaixo. Gostaríamos de encontrar a mediana. | ||
| l = [9,1,0,2,3,4,6,8,7,10,5] | ||
| len (l) == 11, então estamos procurando pelo 6º menor elemento | ||
| Primeiro, devemos escolher um pivô. Selecionamos aleatoriamente o índice 3. | ||
| O valor neste índice é 2. | ||
| Particionamento com base no pivô: | ||
| [1,0,2], [9,3,4,6,8,7,10,5] | ||
| Queremos o 6º elemento. 6-len (esquerda) = 3, então queremos | ||
| o terceiro menor elemento na matriz certa | ||
| Agora estamos procurando o terceiro menor elemento na matriz abaixo: | ||
| [9,3,4,6,8,7,10,5] | ||
| Escolhemos um índice aleatoriamente para ser nosso pivô. | ||
| Escolhemos o índice 3, o valor em que, l [3] = 6 | ||
| Particionamento com base no pivô: | ||
| [3,4,5,6] [9,7,10] | ||
| Queremos o terceiro menor elemento, então sabemos que é o | ||
| 3º menor elemento na matriz esquerda | ||
| Agora estamos procurando o terceiro menor na matriz abaixo: | ||
| [3,4,5,6] | ||
| Escolhemos um índice aleatoriamente para ser nosso pivô. | ||
| Escolhemos o índice 1, o valor em que, l [1] = 4 | ||
| Particionamento com base no pivô: | ||
| [3,4] [5,6] | ||
| Estamos procurando o item no índice 3, então sabemos que é | ||
| o menor na matriz certa. | ||
| Agora estamos procurando o menor elemento na matriz abaixo: | ||
| [5,6] | ||
| Neste ponto, podemos ter um caso base que escolhe a maior | ||
| ou item menor com base no índice. | ||
| Estamos procurando o menor item, que é 5. | ||
| retorno 5 | ||
| Esse algoritmo roda em O(n) | ||
| http://people.csail.mit.edu/rivest/pubs/BFPRT73.pdf | ||
| """ |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.