Answer to Question #109038 in Python for Gnanamani CT

Question #109038
Sort the given list of elements using quick sort.
12,8,6,13,56,78,3,46
1
Expert's answer
2020-04-14T01:46:39-0400
def quicksort(nums):
   import random
   if len(nums) <= 1:
       return nums
   else:
       q = random.choice(nums)
   l_nums = [n for n in nums if n < q]
 
   e_nums = [q] * nums.count(q)
   b_nums = [n for n in nums if n > q]
   return quicksort(l_nums) + e_nums + quicksort(b_nums)

print(quicksort([12, 8, 6, 13, 56, 78, 3, 46]))


This is a recursive solution, it works faster

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS