Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- printf("Hello world!\n");
- int arreglo[100]; // <-- capacidad fisica el arreglo
- int validos = 0; // <-- cantidad de elementos cargados
- char mander = 's';
- while ((validos<100) && (mander == 's'))
- {
- printf("ingrese un numero\n");
- scanf("%d",&arreglo[validos]);
- validos++;
- printf("ingrese s para seguir \n");
- fflush(stdin);
- scanf("%c",&mander);
- }
- int indice = 0;
- while (indice < validos)
- {
- printf("%d\n",arreglo[indice]);
- indice++;
- }
- ordenacionSeleccion(arreglo,validos);
- indice = 0;
- printf("ordenado!\n");
- while (indice < validos)
- {
- printf("%d\n",arreglo[indice]);
- indice++;
- }
- return 0;
- }
- void ordenacionSeleccion(int arreglo[], int validos)
- {
- int posMenor = 0;
- int aux = 0;
- int indice = 0;
- while (indice<validos)
- {
- posMenor = posmenor(arreglo,indice,validos);
- //aca tengo la pos del menor
- //intercambio
- aux = arreglo[posMenor]; //DONDE esta el menor
- //aux ahora es el menor
- arreglo[posMenor] = arreglo[indice]; //me llevo el actual hacia la posMenor
- //me llevo el "más" grande (actual) hacia donde estaba el menor
- arreglo[indice] = aux; //el menor viene a la pos actual
- //actual > menor
- indice++; //avanzo para buscar otro menor
- //los menores sobre el lado izquierdo "ordenados"
- }
- }
- //busco el menor pero a partir de una pos que es posInicial
- int posmenor(int arreglo[], int posInicial, int validos)
- {
- int menor = arreglo[posInicial];//asumo que esta ahi
- int posMenor = posInicial; //me guardo la pos
- int indice = posInicial + 1; //busco en esa pos + 1
- while (indice < validos) //hasta el fin del arreglo
- {
- if (menor > arreglo[indice]) //menor me mintio! :(
- {
- //reemplazos
- menor = arreglo[indice]; //para comparar
- posMenor = indice; //para retornar
- }
- indice++;
- }
- return posMenor;
- }
Advertisement
Add Comment
Please, Sign In to add comment