Selection sort (Método por selección)

#include "conio.h" #include <iostream> using namespace std; class Seleccion { private: double numeros[20]; public: int limite, iteraciones; Seleccion(); ~Seleccion(); void Ordenamiento(int,int,int); friend ostream& operator<<(ostream&, Seleccion*); friend istream& operator>>(istream&, Seleccion*); }; Seleccion::Seleccion() { iteraciones = 0; limite = 20; for(int i = 0; i < 20; i++) { numeros[i] = 0; } } Seleccion::~Seleccion() { iteraciones = 0; limite = 0; for(int i = 0; i < 20; i++) { numeros[i] = 0; } } void Seleccion::Ordenamiento(int numeroMenor, int numeroMayor, int tope) { double auxiliar = 0; if(tope != 0) { if(numeroMenor != tope) { if(numeros[numeroMayor] < numeros[numeroMenor]) numeroMayor = numeroMenor; numeroMenor++; Ordenamiento(numeroMenor, numeroMayor, tope); } else { cout << this; iteraciones++; tope--; auxiliar = numeros[numeroMayor]; numeros[numeroMayor] = numeros[tope]; numeros[tope] = auxiliar; Ordenamiento(0, 1, tope); } } } ostream& operator<<(ostream& output, Seleccion* o) { for(int i = 0; i < o->limite; i++) { if(i != 0) cout << ", "; cout << o->numeros[i]; } cout << endl; return output; } istream& operator>>(istream &input, Seleccion* o) { for(int i = 0; i < o->limite; i++) { cout << " Ingrese el numero " << i + 1 << ":" << endl << " >"; cin >> o->numeros[i]; } cout << endl << "Los datos ingresados son: " << o; return input; } int main() { Seleccion* objeto = new Seleccion(); do { cout << "Ingrese la cantidad de datos a ingresar:" << endl << ">"; cin >> objeto->limite; if(objeto->limite > 20 || objeto->limite < 2) cout << "ERROR: Cantidad de datos no valida." << endl << "Debe ingresar por lo menos 2 numeros y maximo 20." << endl << endl; else break; } while(true); cout << endl << endl; cin >> objeto; cout << endl << endl; objeto->Ordenamiento(0, 1, objeto->limite); cout << endl << endl; cout << "Numero de iteraciones: " << objeto->iteraciones; _getch(); return 0; }
A 100% functional code of the selection sort. The code is in Spanish, but I think it's readable.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.