0

I am working on the QuickSort - Median Three Algorithm. I have no problem with the first and last element sorting. But, when comes to the Median-three, I am slightly confused. I hope someone could help me on this.

Would be appreciate if someone could provide me some pseudocode?

My understanding is to get the middle index by doing this. (start + end) / 2 , then swap the middle pivot value to the first value, after all these done it should goes well with the normal quick sort ( partitioning and sorting).

Somehow, I couldn't get it works. Please help!

#Array Swap function
def swap(A,i,k):
    temp=A[i]
    A[i]=A[k]
    A[k]=temp

# Get Middle pivot function    
def middle(lista):
    if len(lista) % 2 == 0:
        result=  len(lista) // 2 - 1
    else:
        result =  len(lista)  // 2
    return result

def median(lista):
    if len(lista) % 2 == 0:
        return sorted(lista)[len(lista) // 2 - 1]
    else:
        return sorted(lista)[len(lista) // 2]


# Create partition function
def partition(A,start,end):

    m = middle(A[start:end+1])
    medianThree = [ A[start], A[m], A[end] ]

    if A[start] == median(medianThree):

       pivot_pos = start

    elif A[m] == median(medianThree):
       tempList = A[start:end+1]
       pivot_pos = middle(A[start:end+1])
       swap(A,start,pivot_pos+start)

    elif A[end] == median(medianThree):

       pivot_pos = end



    #pivot = A[pivot_pos]
    pivot = pivot_pos
    # swap(A,start,end) // This line of code is to switch the first and last element pivot
    swap(A,pivot,end)
    p = A[pivot]
    i = pivot + 1
    for j in range(pivot+1,end+1):
        if A[j] < p:
            swap(A,i,j)
            i+=1
    swap(A,start,i-1)
    return i-1


count = 0
#Quick sort algorithm
def quickSort(A,start,end):
    global tot_comparisons

    if start < end:
       # This to create the partition based on the 
       pivot_pos = partition(A,start,end)
       tot_comparisons += len(A[start:pivot_pos-1]) + len(A[pivot_pos+1:end])
       # This to sort the the left partition
       quickSort(A,start,pivot_pos -1)
       #This to sort the right partition
       quickSort(A,pivot_pos+1,end)
B David
  • 43
  • 9
  • 1
    Possible duplicate of [median of three values strategy](https://stackoverflow.com/questions/7559608/median-of-three-values-strategy) – Robin Green Nov 17 '18 at 08:39
  • Please provide the code of what you already have done to get it working? – Saqib Shahzad Nov 17 '18 at 09:13
  • @SaqibShahzad - My god feel tells me that it must be the partitioning function went wrong. Please help me to debug this and find out the root caused. – B David Nov 17 '18 at 10:51
  • (I think function `middle()` weird: is its result any different from `len(lista) // 2`?) Considering you put pivot determination in `partition()`, too, your notion has an excellent chance of being true. – greybeard Nov 17 '18 at 10:55
  • @greybeard - I got it. Let me give a try on this. Are you suggesting I should change the middle function to only return only return -> len(lista) // 2? – B David Nov 17 '18 at 11:21
  • Then reason is to handle the odd and even list array. – B David Nov 17 '18 at 11:27

0 Answers0