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
  • PYTHON: Do not use the list sort method or sorted function. Given a list of numbers...

    PYTHON: Do not use the list sort method or sorted function. Given a list of numbers in random order, write an algorithm that works in O(n2) to sort the list. Do not use the list sort method or sorted function. Please DO NOT USE list_sort method. the last answer did not work.

  • 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...

  • Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...

    Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here

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