- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 49.1k
Added Kth largest element algorithm #10687
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            24 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      e6c2be5
              
                neha3423
              
              
                neha3423 93f2d3e
              
                neha3423
              
              
                neha3423 53fb687
              
                neha3423
              
              
                neha3423 a50bd08
              
                Merge branch 'TheAlgorithms:master' into master
              
              
                neha3423 d78fa11
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] bd6d47c
              
                neha3423
              
              
                neha3423 2ce0e9c
              
                neha3423
              
              
                neha3423 41312a1
              
                neha3423
              
              
                neha3423 b4e58a8
              
                Merge branch 'TheAlgorithms:master' into master
              
              
                neha3423 add638b
              
                Merge branch 'master' of https://github.com/neha3423/Python
              
              
                neha3423 0b19aae
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 9e312fa
              
                neha323
              
              
                neha3423 36490ef
              
                Merge branch 'master' of https://github.com/neha3423/Python
              
              
                neha3423 0cb88d4
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] b76a792
              
                neha3423
              
              
                neha3423 ebd88b9
              
                Merge branch 'master' of https://github.com/neha3423/Python
              
              
                neha3423 3bdaa1f
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] bb6a1a8
              
                neha3423
              
              
                neha3423 7bb2f6b
              
                Merge branch 'master' of https://github.com/neha3423/Python
              
              
                neha3423 fb5c506
              
                neha3423
              
              
                neha3423 2adea82
              
                neha3423
              
              
                neha3423 f750a7e
              
                neha3423
              
              
                neha3423 49fdc86
              
                Added test case for tuple
              
              
                neha3423 7d35911
              
                Update kth_largest_element.py
              
              
                cclauss File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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,74 @@ | ||
| """ | ||
| Given an array of integers and an integer k, find the kth largest element in the array | ||
|  | ||
| https://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in-on | ||
| """ | ||
|  | ||
|  | ||
| def partition(arr, low, high): | ||
|         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| """ | ||
| Partitions list based on the pivot element | ||
|         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| Args: | ||
| arr: The list to be partitioned | ||
| low: The lower index of the list | ||
| high: The higher index of the list | ||
| Returns: | ||
| int: The index of pivot element after partitioning | ||
|  | ||
| Examples: | ||
| >>> partition([3,1,4,5,9,2,6,5,3,5],0,9) | ||
| 4 | ||
| >>> partition([7,1,4,5,9,2,6,5,8],0,8) | ||
| 1 | ||
|         
                  neha3423 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| """ | ||
| pivot = arr[high] | ||
| i = low - 1 | ||
| for j in range(low, high): | ||
| if arr[j] >= pivot: | ||
| i += 1 | ||
| arr[i], arr[j] = arr[j], arr[i] | ||
| arr[i + 1], arr[high] = arr[high], arr[i + 1] | ||
| return i + 1 | ||
|  | ||
|  | ||
| def kth_largest_element(arr, k): | ||
|         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| """ | ||
| Finds the kth largest element in a list. | ||
|  | ||
| Args: | ||
| nums : The list of numbers. | ||
| k : The position of the desired kth largest element. | ||
|  | ||
| Returns: | ||
| int: The kth largest element. | ||
|  | ||
| Examples: | ||
| >>> kth_largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], 3) | ||
|         
                  neha3423 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| 5 | ||
| >>> kth_largest_element([2, 5, 6, 1, 9, 3, 8, 4, 7, 3, 5], 1) | ||
| 9 | ||
| >>> kth_largest_element([9, 1, 3, 6, 7, 9, 8, 4, 2, 4, 9], 11) | ||
| 1 | ||
|         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| >>> kth_largest_element([1, 2, 4, 3, 5, 9, 7, 6, 5, 9, 3], 0) | ||
| 'Invalid value of k' | ||
|         
                  neha3423 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| """ | ||
| if not 1 <= k <= len(arr): | ||
| return "Invalid value of k" | ||
| low, high = 0, len(arr) - 1 | ||
| while low <= high: | ||
| if low > len(arr) - 1 or high < 0: | ||
| return "Invalid value of k" | ||
| pivot_index = partition(arr, low, high) | ||
| if pivot_index == k - 1: | ||
| return arr[pivot_index] | ||
| elif pivot_index > k - 1: | ||
| high = pivot_index - 1 | ||
| else: | ||
| low = pivot_index + 1 | ||
| return "Kth largest element not found" | ||
|  | ||
|  | ||
| if __name__ == "__main__": | ||
| import doctest | ||
|  | ||
| doctest.testmod() | ||
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.