Data Structure Visualization

image.png

image.png

Quick Sort Algorithm

image.png


lst = [3,9,8,12,4,2,5,7,6]

print(lst)

pivot = lst[-1] 

i = -1 

for j in range(len(lst)):
    if lst[j] < pivot : 
        i += 1 
        lst[i] , lst[j] = lst[j] , lst[i] 

lst[i+1] , lst[-1] = lst[-1] , lst[i+1]

print(lst)

def partition(lst , low , high):

    pivot = lst[high] 

    i = low-1

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

    lst[i+1] , lst[high] = lst[high] , lst[i+1]

    print(lst)

lst = [3,9,8,12,4,2,5,7,6]

print(lst)

partition(lst , 2 , 6)

image.png


def partition(lst , low , high):

    pivot = lst[high] 

    i = low-1

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

    lst[i+1] , lst[high] = lst[high] , lst[i+1]

    return i+1

def quick_sort(lst , low , high) : 

    if low < high : 

        pi = partition(lst , low , high) 

        quick_sort(lst , low , pi - 1) 
        quick_sort(lst , pi+1 , high)

lst = [3,9,8,12,4,2,5,7,6]

print(lst)

# partition(lst , 2 , 6)

quick_sort(lst , 0 , len(lst)-1)

print(lst)