Skip to content

Quick sort and partition functions error  #4

@KODE1047

Description

@KODE1047

Hi !
In the codes of partition and quick sort in 06_03_Quick_Sort there is some errors. There is no swap() function in python, you should import random library and then use its functions, there is no rand() function in random its randint(), in arguments of quick sort there no part for getting array.

import random
def partition(A,left, right):
    leftPointer = left
    rightPointer = right-1
    A[right], A[random.randint(left,right)] = A[random.randint(left,right)], A[right]
    pivot = A[right]
    while True:
        while leftPointer<=rightPointer and A[leftPointer] <= pivot :
            leftPointer=leftPointer+1
        while rightPointer >= leftPointer and A[rightPointer] > pivot :
             rightPointer=rightPointer-1
        if leftPointer >= rightPointer :
             break
        else :
            A[leftPointer],A[rightPointer] = A[rightPointer], A[leftPointer] 

    A[leftPointer], A[right] = A[right], A[leftPointer]

    return leftPointer, pivot`
def quickSort(A, left, right):
    if right <= left:
        return
    index, pivot = partition(A,left, right)
    print("left=",left,"right=",right,"pivot=",pivot,"index=",index)
    print(A)
    print("")
    quickSort(A,left,index-1)
    quickSort(A,index+1,right) 

A=[19,97,22,50,93,31,72,11,46]

quickSort(A,0,8)

I appreciate for your great teaching, I skip my classes to just sit and learn from you :)
I really hope one day I can thank you in person.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions