Algoritmo de Ordenação Radix
Publicado por João Cristiano Monteiro da Silva (última atualização em 07/07/2011)
[ Hits: 7.245 ]
Essa classe implementa um paradigma de ordenação usado originalmente para ordenação de cartões perfurados, onde os valores são empilhados dinamicamente.
Em suma, esse método agrupa os elementos levando em consideração um conjunto de combinações, levando em consideração o valor específico de cada algarismo.
#include <iostream> #include <stdlib.h> #include <string.h> #include <math.h> #include <vector> using namespace std; class Radix{ private: int *vetor; int n; public: Radix(int size){ this->vetor = new int[size]; this->n = size; memset(vetor, 0, size*sizeof(vetor)); } Radix(int *origem, int size){ this->n = size; vetor = origem; } int *getVetor(){ return(this->vetor); } int getVetorSize(){ return(this->n); } void ordenacaoRadix(){ vector<int> pilha[10]; int iCont = 0; int max = getDigInteiros(getMaxValue()); int alg = 1; do { for(int i = 0; i < 10 && !iCont; i++) pilha[i].clear(); pilha[getAlgRangeInt(vetor[iCont], alg)].push_back(vetor[iCont]); iCont++; if(iCont >= this->n){ iCont = 0; for(int i = 0; i < 10; i++){ for(int j = 0; j < pilha[i].size(); j++){ vetor[iCont++] = pilha[i][j]; } } iCont = 0; alg++; } } while(pilha[0].size() != this->n || (max+1) >= alg); } private: int getMaxValue(){ int maior = 0; for(int i = 1; i < this->n; i++){ if(*(vetor + i) > *(vetor + maior)){ maior = i; } } return(*(vetor + maior)); } int getAlgRangeInt(int valor, int algarismo){ unsigned quociente, divisor; quociente = (int) pow(10, algarismo); divisor = (int) pow(10, algarismo - 1); return ((valor % quociente) / divisor); } int getDigInteiros(int valor){ if(valor<10) return 1; return(1 + getDigInteiros(valor / 10)); } }; int main(int argc, char **argv){ int vetor[] = {20, 10, 2348, 22, 2, 50, 80, 5}; Radix radix(vetor, 8); radix.ordenacaoRadix(); int *retorno = radix.getVetor(); for(int i = 0; i < 8; i++){ cout << (i + 1) << ": " << *(retorno + i) << endl; } return(EXIT_SUCCESS); }
Desenhando Nuvens ou o Fractal de Plasma
Lista simplesmente encadeada com busca auto-organizada
Nenhum comentário foi encontrado.
Resolver problemas de Internet
Como compartilhar a tela do Ubuntu com uma Smart TV (LG, Samsung, etc.)
Descritores de Arquivos e Swappiness
Fez porcaria no teu repositório Git? Aprenda a restaurar uma versão anterior do seu código!
Restaurando Fontes de Download do Hydra no Linux
Atualizando "na marra" o YT-DLP quando começa a dar erro de downloads
Como instalar o WPS com interface e corretor ortográfico em PT-BR no Arch Linux
How to Choose a Motherboard? (0)
Atualizador de Programas do Zorin 17.3 não funciona [RESOLVIDO] (5)