Fandom

Scratchpad

PLE:Unidad7Ejercicio02

215,999pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

/*
Programa:       Unidad7Ejercicio02.cpp
Descripción:    Crea un array dinámico y ordena mediante el método de la selección directa.
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;

void ordena(int *array, int t);

int main ()
{
        clock_t ini, fin;
        int *num, i, n, pos, tam;

        srand(time(NULL));

        cout << "Numero de elemntos del arraty ";
        cin >> tam;

        num = new int[tam];

        // Generamos valores aleatorios
        for(i= 0; i < tam; i++)
        {
                *(num+i) = rand() % 100;
        }

        // Mostramos el array desordenado
        for(i= 0; i < tam; i++)
        {
                cout << *(num+i) << '\t';
        }

        ini = clock();
        // Ordena el array
        ordena(num, tam);
        fin = clock();

        // Mostramos el array ordenado
        cout << endl << "Array ordenado" << endl;
        for(i= 0; i < tam; i++)
        {
                cout << *(num+i) << '\t';
        }

        cout << "\nTiempo transcurrido \n" << (fin-ini)/(double) CLOCKS_PER_SEC << endl;
        cout << "Fin = " << fin << endl;
        cout << "Ini = " << ini << endl;
        cout << "C_P_S = " << CLOCKS_PER_SEC << endl;

        system("pause");
}

void ordena(int *array, int t)
{
        int i,j,k,aux;
        for (i = 0; i < t-1; i++)
        {
	        k = i;
        	aux = *(array+i);
	        for (j = i+1; j <= t-1; j++)
        	{
	        	if (*(array+j) < aux)
		        {
			        k = j;
        			aux = *(array+j);
	        	}
        	}
	        *(array+k) = *(array+i);
        	*(array+i) = aux;
        }
}

Also on Fandom

Random wikia