Sorting objects using In | Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Let 's represent objects as strings
objects = [ ] NEW_LINE hash = dict ( ) NEW_LINE def partition ( l , r ) : NEW_LINE INDENT global objects , hash NEW_LINE j = l - 1 NEW_LINE last_element = hash [ objects [ r ] ] NEW_LINE for i in range ( l , r ) : NEW_LINE INDENT if ( hash [ objects [ i ] ] <= last_element ) : NEW_LINE INDENT j += 1 NEW_LINE ( objects [ i ] , objects [ j ] ) = ( objects [ j ] , objects [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE ( objects [ j ] , objects [ r ] ) = ( objects [ r ] , objects [ j ] ) NEW_LINE return j NEW_LINE DEDENT def quicksort ( l , r ) : NEW_LINE INDENT if ( l < r ) : NEW_LINE INDENT mid = partition ( l , r ) NEW_LINE quicksort ( l , mid - 1 ) NEW_LINE quicksort ( mid + 1 , r ) NEW_LINE DEDENT DEDENT def sortObj ( ) : NEW_LINE INDENT global objects , hash NEW_LINE hash [ " blue " ] = 1 NEW_LINE hash [ " red " ] = 2 NEW_LINE hash [ " yellow " ] = 3 NEW_LINE quicksort ( 0 , int ( len ( objects ) - 1 ) ) NEW_LINE for i in objects : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT objects = [ " red " , " blue " , " red " , " yellow " , " blue " ] NEW_LINE sortObj ( ) NEW_LINE