Sorting objects using In | C ++ implementation of the approach ; 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 ; Create a hash table ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Driver code ; Let 's represent objects as strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; int partition ( vector < string > & objects , int l , int r , unordered_map < string , int > & hash ) { int j = l - 1 ; int last_element = hash [ objects [ r ] ] ; for ( int i = l ; i < r ; i ++ ) { if ( hash [ objects [ i ] ] <= last_element ) { j ++ ; swap ( objects [ i ] , objects [ j ] ) ; } } j ++ ; swap ( objects [ j ] , objects [ r ] ) ; return j ; } void quicksort ( vector < string > & objects , int l , int r , unordered_map < string , int > & hash ) { if ( l < r ) { int mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } void sortObj ( vector < string > & objects ) { unordered_map < string , int > hash ; hash [ " blue " ] = 1 ; hash [ " red " ] = 2 ; hash [ " yellow " ] = 3 ; quicksort ( objects , 0 , int ( objects . size ( ) - 1 ) , hash ) ; for ( int i = 0 ; i < objects . size ( ) ; i ++ ) cout << objects [ i ] << " ▁ " ; } int main ( ) { vector < string > objects { " red " , " blue " , " red " , " yellow " , " blue " } ; sortObj ( objects ) ; return 0 ; }