Clasificación de objetos usando el algoritmo de clasificación In-Place

Dada una array de objetos rojos , azules y amarillos , la tarea es usar un algoritmo de ordenación en el lugar para ordenar la array de tal manera que todos los objetos azules aparezcan antes que todos los objetos rojos y todos los objetos rojos aparezcan antes que todos los objetos. objetos amarillos
Ejemplos: 
 

Entrada: arr[] = {“azul”, “rojo”, “amarillo”, “azul”, “amarillo”} 
Salida: azul azul rojo amarillo amarillo
Entrada: arr[] = {“rojo”, “azul”, “ rojo”, “amarillo”, “azul”} 
Salida: azul azul rojo rojo amarillo 
 

Enfoque: en primer lugar , asigne los valores de los objetos azul , rojo y amarillo a 1 , 2 y 3 respectivamente utilizando una tabla hash. Ahora use estos valores asignados cada vez que se requiera una comparación de dos objetos. Entonces, el algoritmo ordenará la array de objetos de modo que todos los objetos azules (asignación al valor 1) aparecerán primero, luego todos los objetos rojos (asignación al valor 2) y luego todos los objetos amarillos (asignación al valor 3).
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Partition function which will partition
// the array and into two parts
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++) {
 
        // 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<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);
    }
}
 
// Function to sort and print the objects
void sortObj(vector<string>& objects)
{
 
    // Create a hash table
    unordered_map<string, int> 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<string> 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<String> objects, int l, int r,
                        Map<String, Integer> 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<String> objects, int l, int r,
                         Map<String, Integer> 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<String> objects)
{
 
    // Create a hash table
    Map<String, Integer> 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<String> 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<String> objects, int l, int r,
                           Dictionary<String, int> 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<String> objects, int l, int r,
                            Dictionary<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);
    }
}
 
// Function to sort and print the objects
static void sortObj(List<String> objects)
{
 
    // Create a hash table
    Dictionary<String,
               int> hash = new Dictionary<String,
                                          int>();
 
    // 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<String> objects = new List<String>{"red", "blue",
                                            "red", "yellow",
                                            "blue"};
 
    sortObj(objects);
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
// Javascript implementation of the approach
 
// Partition function which will partition
// the array and into two parts
function partition(objects, l, r, hash)
{
    let j = l - 1;
   
    let last_element = hash.get(objects[r]);
   
    for (let i = l; i < r; i++)
    {
   
        // Compare hash values of objects
        if (hash.get(objects[i]) <= last_element)
        {
            j++;
            let temp = objects[i];
            objects[i] = objects[j];
            objects[j] = temp;
        }
    }
   
    j++;
   
    let temp = objects[r];
            objects[r] = objects[j];
            objects[j] = temp;
   
    return j;
}
 
// Classic quicksort algorithm
function quicksort(objects, l, r, hash)
{
    if (l < r)
    {
        let 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
function sortObj(objects)
{
 
    // Create a hash table
    let hash = new Map();
   
    // As the sorting order is blue objects,
    // red objects and then yellow objects
    hash. set("blue", 1);
    hash. set("red", 2);
    hash. set("yellow", 3);
   
    // Quick sort function
    quicksort(objects, 0, objects.length - 1, hash);
   
    // Printing the sorted array
    for (let i = 0; i < objects.length; i++)
        document.write(objects[i] + " ");
}
 
// Driver code
let objects = ["red", "blue","red", "yellow", "blue"];
sortObj(objects);
 
// This code is contributed by patel2127
</script>
Producción: 

blue blue red red yellow

 

Complejidad de tiempo: O(n^2) desde que usé qucksort

Publicación traducida automáticamente

Artículo escrito por Aakash_Panchal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *