Menu
×
×
Correct!

Exercise:

Complete the code for the Quicksort algorithm.

def partition(array, low, high): pivot = array[high] i = low - 1 for j in range(low, high): if array[j] <= pivot: i += 1 array[i], array[j] = array[j], array[i] array[i+1], array[high] = array[high], array[i+1] return i+1 def quicksort(array, low=0, high=None): if high is None: high = len(array) - 1 if low < high: pivot_index = partition(array, low, high) quicksort(array, low, pivot_index-1) quicksort(array, pivot_index+1, high) my_array = [64, 34, 25, 12, 22, 11, 90, 5] quicksort(my_array) print("Sorted array:", my_array)

Not Correct

Click here to try again.

Correct!

Next ❯
def partition(array, low, high):
    pivot = array[high]
    i = low - 1

    for j in range(low, high):
        if array[j] <= pivot:
            i += 1
            array[i], array[j] = array[j], array[i]

    array[i+1], array[high] = array[high], array[i+1]
    return i+1

def quicksort(array, low=0, high=None):
    if high is None:
        high = len(array) - 1

    if low < high:
        pivot_index = partition(array, low, high)
        (array, low, pivot_index-1)
        (array, pivot_index+1, high)

my_array = [64, 34, 25, 12, 22, 11, 90, 5]
quicksort(my_array)
print("Sorted array:", my_array)

    
  




Log in to keep your progress
Sign up to keep your progress

Completed 0 of 43 Exercises:

DSA Get started
DSA Arrays
DSA Bubble Sort
DSA Selection Sort
DSA Quick Sort
DSA Counting Sort
DSA Radix Sort
DSA Linear Search
DSA Binary Search
DSA Linked Lists
DSA Stacks
DSA Queues
DSA Trees
DSA Binary Trees
DSA Binary Search Trees
DSA AVL Trees
DSA Graphs
DSA Cycle Detection
DSA Dijkstra
DSA Bellman-Ford

×

Reset the Score?

This will reset the score of ALL 43 exercises.

Are you sure you want to continue?



×

Congratulations!

You have finished all 43 DSA exercises.

Share your score: