Skip to content
Related Articles
Sorting objects using In-Place sorting algorithm
• Last Updated : 09 Jun, 2021

Given an array of red, blue and yellow objects, the task is to use an in-place sorting algorithm to sort the array in such a way that all the blue objects appear before all the red objects and all the red objects appear before all the yellow objects.
Examples:

Input: arr[] = {“blue”, “red”, “yellow”, “blue”, “yellow”}
Output: blue blue red yellow yellow
Input: arr[] = {“red”, “blue”, “red”, “yellow”, “blue”}
Output: blue blue red red yellow

Approach: First of all map the values of blue, red and yellow objects to 1, 2 and 3 respectively using a hash table. Now use these mapped values whenever a comparison of two objects is required. So, the algorithm will sort the array of objects such that all blue objects ( mapping to value 1 ) will appear first, then all red objects ( mapping to value 2 ) and then all yellow objects ( mapping to value 3 ).
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Partition function which will partition``// the array and into two parts``int` `partition(vector& objects, ``int` `l, ``int` `r,``            ``unordered_map& hash)``{``    ``int` `j = l - 1;` `    ``int` `last_element = hash[objects[r]];` `    ``for` `(``int` `i = l; i < r; i++) {` `        ``// Compare hash values of objects``        ``if` `(hash[objects[i]] <= last_element) {``            ``j++;``            ``swap(objects[i], objects[j]);``        ``}``    ``}` `    ``j++;` `    ``swap(objects[j], objects[r]);` `    ``return` `j;``}` `// Classic quicksort algorithm``void` `quicksort(vector& objects, ``int` `l, ``int` `r,``                    ``unordered_map& hash)``{``    ``if` `(l < r) {``        ``int` `mid = partition(objects, l, r, hash);``        ``quicksort(objects, l, mid - 1, hash);``        ``quicksort(objects, mid + 1, r, hash);``    ``}``}` `// Function to sort and print the objects``void` `sortObj(vector& objects)``{` `    ``// Create a hash table``    ``unordered_map hash;` `    ``// As the sorting order is blue objects,``    ``// red objects and then yellow objects``    ``hash[``"blue"``] = 1;``    ``hash[``"red"``] = 2;``    ``hash[``"yellow"``] = 3;` `    ``// Quick sort function``    ``quicksort(objects, 0, ``int``(objects.size() - 1), hash);` `    ``// Printing the sorted array``    ``for` `(``int` `i = 0; i < objects.size(); i++)``        ``cout << objects[i] << ``" "``;``}` `// Driver code``int` `main()``{` `    ``// Let's represent objects as strings``    ``vector objects{ ``"red"``, ``"blue"``,``                            ``"red"``, ``"yellow"``, ``"blue"` `};` `    ``sortObj(objects);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;``class` `GFG``{` `// Partition function which will partition``// the array and into two parts``static` `int` `partition(Vector objects, ``int` `l, ``int` `r,``                        ``Map hash)``{``    ``int` `j = l - ``1``;` `    ``int` `last_element = hash.get(objects.get(r));` `    ``for` `(``int` `i = l; i < r; i++)``    ``{` `        ``// Compare hash values of objects``        ``if` `(hash.get(objects.get(i)) <= last_element)``        ``{``            ``j++;``            ``Collections.swap(objects, i, j);``        ``}``    ``}` `    ``j++;` `    ``Collections.swap(objects, j, r);` `    ``return` `j;``}` `// Classic quicksort algorithm``static` `void` `quicksort(Vector objects, ``int` `l, ``int` `r,``                         ``Map hash)``{``    ``if` `(l < r)``    ``{``        ``int` `mid = partition(objects, l, r, hash);``        ``quicksort(objects, l, mid - ``1``, hash);``        ``quicksort(objects, mid + ``1``, r, hash);``    ``}``}` `// Function to sort and print the objects``static` `void` `sortObj(Vector objects)``{` `    ``// Create a hash table``    ``Map hash = ``new` `HashMap<>();` `    ``// As the sorting order is blue objects,``    ``// red objects and then yellow objects``    ``hash. put(``"blue"``, ``1``);``    ``hash. put(``"red"``, ``2``);``    ``hash. put(``"yellow"``, ``3``);` `    ``// Quick sort function``    ``quicksort(objects, ``0``, objects.size() - ``1``, hash);` `    ``// Printing the sorted array``    ``for` `(``int` `i = ``0``; i < objects.size(); i++)``        ``System.out.print(objects.get(i) + ``" "``);``}` `// Driver code``public` `static` `void` `main(String []args)``{``    ``// Let's represent objects as strings``    ``Vector objects = ``new` `Vector<>(Arrays.asList( ``"red"``, ``"blue"``,``                                                         ``"red"``, ``"yellow"``,``                                                         ``"blue"` `));` `    ``sortObj(objects);``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 implementation of the approach` `# Partition function which will partition``# the array and into two parts``objects ``=` `[]``hash` `=` `dict``()` `def` `partition(l, r):``    ``global` `objects, ``hash``    ``j ``=` `l ``-` `1` `    ``last_element ``=` `hash``[objects[r]]` `    ``for` `i ``in` `range``(l, r):` `        ``# Compare hash values of objects``        ``if` `(``hash``[objects[i]] <``=` `last_element):``            ``j ``+``=` `1``            ``(objects[i],``             ``objects[j]) ``=` `(objects[j],``                            ``objects[i])` `    ``j ``+``=` `1` `    ``(objects[j],``     ``objects[r]) ``=` `(objects[r],``                    ``objects[j])` `    ``return` `j` `# Classic quicksort algorithm``def` `quicksort(l, r):``    ``if` `(l < r):``        ``mid ``=` `partition(l, r)``        ``quicksort(l, mid ``-` `1``)``        ``quicksort(mid ``+` `1``, r)` `# Function to sort and print the objects``def` `sortObj():``    ``global` `objects, ``hash` `    ``# As the sorting order is blue objects,``    ``# red objects and then yellow objects``    ``hash``[``"blue"``] ``=` `1``    ``hash``[``"red"``] ``=` `2``    ``hash``[``"yellow"``] ``=` `3` `    ``# Quick sort function``    ``quicksort(``0``, ``int``(``len``(objects) ``-` `1``))` `    ``# Printing the sorted array``    ``for` `i ``in` `objects:``        ``print``(i, end ``=` `" "``)` `# Driver code` `# Let's represent objects as strings``objects ``=` `[``"red"``, ``"blue"``, ``"red"``,``               ``"yellow"``, ``"blue"``]` `sortObj()` `# This code is contributed``# by Mohit Kumar`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `// Partition function which will partition``// the array and into two parts``static` `int` `partition(List objects, ``int` `l, ``int` `r,``                           ``Dictionary hash)``{``    ``int` `j = l - 1;``    ``String temp;``    ``int` `last_element = hash[objects[r]];` `    ``for` `(``int` `i = l; i < r; i++)``    ``{` `        ``// Compare hash values of objects``        ``if` `(hash[objects[i]] <= last_element)``        ``{``            ``j++;``            ``temp = objects[i];``            ``objects[i] = objects[j];``            ``objects[j] = temp;``        ``}``    ``}` `    ``j++;` `    ``temp = objects[r];``    ``objects[r] = objects[j];``    ``objects[j] = temp;` `    ``return` `j;``}` `// Classic quicksort algorithm``static` `void` `quicksort(List objects, ``int` `l, ``int` `r,``                            ``Dictionary hash)``{``    ``if` `(l < r)``    ``{``        ``int` `mid = partition(objects, l, r, hash);``        ``quicksort(objects, l, mid - 1, hash);``        ``quicksort(objects, mid + 1, r, hash);``    ``}``}` `// Function to sort and print the objects``static` `void` `sortObj(List objects)``{` `    ``// Create a hash table``    ``Dictionary hash = ``new` `Dictionary();` `    ``// As the sorting order is blue objects,``    ``// red objects and then yellow objects``    ``hash.Add(``"blue"``, 1);``    ``hash.Add(``"red"``, 2);``    ``hash.Add(``"yellow"``, 3);` `    ``// Quick sort function``    ``quicksort(objects, 0, objects.Count - 1, hash);` `    ``// Printing the sorted array``    ``for` `(``int` `i = 0; i < objects.Count; i++)``        ``Console.Write(objects[i] + ``" "``);``}` `// Driver code``public` `static` `void` `Main(String []args)``{``    ``// Let's represent objects as strings``    ``List objects = ``new` `List{``"red"``, ``"blue"``,``                                            ``"red"``, ``"yellow"``,``                                            ``"blue"``};` `    ``sortObj(objects);``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``
Output:
`blue blue red red yellow`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up