We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Quicksort 2 - Sorting
  2. Discussions

Quicksort 2 - Sorting

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • ava_xiaohong_de1
    12 months ago+ 0 comments

    Use the Quicksort algorithm to sort the entire array

    def sorting(ar):

    if len(ar) <= 1:
        return ar
    
    left, equal, right = partition(ar)
    merging_arr = sorting(left) + equal + sorting(right)
    print (' '.join(str(e) for e in merging_arr))   
    
    return merging_arr
    

    def partition(ar):

    left, equal, right=[],[ar[0]],[]
    
    for elem in ar[1:]:
        if elem < ar[0]:
            left.append(elem)
        elif elem == ar[0]:
            equal.append(elem)        
        else:
            right.append(elem)
    
    return left, equal, right
    

    if name == 'main':

    n = input()
    
    ar = list(map(int,input().rstrip().split()))
    
    sorting(ar)
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy