Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

quick sort python

def quick_sort(array):
    sort(array, 0, len(array) - 1)


def sort(array, left, right):
    if left >= right:
        return array
    pivot = array[(left + right) // 2]
    index = partition(array, left, right, pivot)

    sort(array, left, index - 1)
    sort(array, index, right)


def partition(array, left, right, pivot):
    while left <= right:
        while array[left] < pivot:
            left += 1
        while array[right] > pivot:
            right -= 1

        if left <= right:
            array[left], array[right] = array[right], array[left]
            left += 1
            right -= 1

    return left


arr = [7, 2, 4, 0, 1, 5, 8, 3, 2, 0]
quick_sort(arr)
print(arr) # [0, 0, 1, 2, 2, 3, 4, 5, 7, 8]
 
PREVIOUS NEXT
Tagged: #quick #sort #python
ADD COMMENT
Topic
Name
1+1 =