Question

Please solve this problem using Python Write a quicksort method that accept a list of numbers...

Please solve this problem using Python

Write a quicksort method that accept a list of numbers and use list comprehension to construct the method. Note that you need to sort the numbers in descending order .
You MUST use List Comprehension to do the sorting

Your program should contain the function with format shown as below:
def quicksort(a):
# Your codes here. Use List Comprehension and return the result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code-

def quicksort(a):
#quicksort using list comprehensions
if a == []: #if list is empty
return []
else:
pivot = a[0]
greater = quicksort([x for x in list[1:] if x < pivot])
lesser = quicksort([x for x in list[1:] if x >= pivot])
return lesser + [pivot] + greater

numbers = (1,6,3,32,8,23,9,12,20,33)
print('sorted list in descending order is:')
print(qsort1(numbers))

Output-

sorted list in descending order is:
[33, 32, 23, 20, 12, 9, 8, 6, 3, 1]

Code-

1 def quicksort(a): #quicksort using list comprehensions if a ] return [] else: 2 3 #if list is empty 4 6 pivot greater lesse

Output-

sorted list in descending order is [33, 32, 23, 20, 12, 9, 8, 6, 3, 1]

Add a comment
Know the answer?
Add Answer to:
Please solve this problem using Python Write a quicksort method that accept a list of numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has...

    Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has size 15 or less; then use INSERTIONSORT on the sublist. Generate 10 random lists of 100 numbers. Sort each list using QUICKSORT and then sort the same list using the combination of QUICKSORT and INSERTIONSORT as described above. Compare the times for each of the two algorithms. You can measure the duration of the time which each algorithm takes as follows: Instant first =...

  • PYTHON Fill in 3 numbers to complete the following 'list', as shown: 35 62 ___ 40...

    PYTHON Fill in 3 numbers to complete the following 'list', as shown: 35 62 ___ 40 ___ 18 ___ 1 55 28 add the month, day, and year you were born in the 3 spaces shown (in that order). (Use a 4-digit year.) Using the list of numbers above, write 4 different sort trace tables like those produced by the practice program linked above: Sort using the BubbleSort algorithm in ascending order. Sort using the BubbleSort algorithm in descending order....

  • 2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers):...

    2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers): result = [] for i in listOfNumbers: if i<0 == 0: result.append(i*i) else: result.append((i*i)+1) return result First, study and test processList(listOfNumbers) to determine what it does Then rewrite its body so that it accomplishes the same task with a one-line list comprehension. Thus, the resulting function will have exactly two lines, the def line and a return line containing a list comprehension expression. 3....

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

  • use python Write a function that will sort a given list using merge sort. You must...

    use python Write a function that will sort a given list using merge sort. You must use the merge sort algorithm (but may be recursive or iterative). The function will take a list as an input and return a sorted version of the list (you may assume it will be a list of integers). The method signature must be merge_sort(lst).

  • Python Implement a class named BubbleStringList. In this class implement a method called add, that when...

    Python Implement a class named BubbleStringList. In this class implement a method called add, that when given a string, it adds it to an internal list object. You can use the list object from the standard python library for the internal list. Next, implement a sort method that uses a BubbleSort to sort the list when called. Create another class called MergeStringList, that implements the same methods but uses a Merge Sort as the sorting algorithm. Create another class called...

  • Solve This Problem using python and please comment on every line. also, use function to solve...

    Solve This Problem using python and please comment on every line. also, use function to solve this. thanks The result will be like as below: Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following functions in the program: This function should accept five test scores as arguments and return the average of the scores. This function should accept a...

  • Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...

    Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...

  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT