Selection Sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /* -Victor de la Rocha -vyk2rr@gmail.com -www.algoritmia.info -Jueves 10/AGO/05-Programa 3, Practica 1 -Selection Sort (Ordenamiento por seleccion) -Program that orders an adjustment with the arrangement algorithm 'selection sort' -Programa que ordena un arreglo con el algoritmo de ordenacion 'ordenamiento por seleccion' */#include <stdio.h> #include <stdlib.h> #include <conio.h> int fillrand(int A[],int N,int R){ int c; for(c=1;c<=N;c++){ A[c]=rand()%R; } return 1; } int selectionsort(int A[],int N){ int i,menor,k,j; for(i=1;i<=N-1;i++){ menor=A[i]; k=i; for(j=i+1;i<=N;i++){ if(A[j]<menor){ menor=A[j]; k=j; } } A[k]=A[i]; A[i]=menor; } return 1; } int salida(int A[],int N){ int c; for(c=1;c<=N;c++){ printf("%d, ",A[c]); } return 1; } void main(){ int A[10]; randomize(); clrscr(); fillrand(A,10,100); printf("SELECTION SORTn"); printf("Datos a ordenar: n"); salida(A,10); printf("nnDatos ordenados: n"); selectionsort(A,10); salida(A,10); getch(); }/* SELECCION(A,N) {El algoritmo ordena los elementos del arreglo utilizando el método de selección directa. A es un arreglo de N elementos} {I, MENOR, K y J son variables de tipo entero} Repetir con I desde 1 hasta N – 1 Hacer MENOR <-- A[I] y K <-- I 1.1 Repetir con J desde I + 1 hasta N 1.1.1 Si A[J] < MENOR entonces Hacer MENOR <-- A[J] y K <-- J 1.1.2 {Fin del condicional del paso 1.1.1} 1.2 {Fin del condicional del paso 1.1} Hacer A[K] <-- A[I] y A[I] <-- MENOR 2. {Fin del ciclo del paso 1} */ |
Arreglo original
140, 10, 5, 9, 16, 8, 0, 1, 52
Arreglo ordenado
0, 1, 5, 8, 9, 10, 16, 52, 140
Elber
2010-04-21 02:28:16
Hola, muy buen sitio, solo que encontre un error en el 'for' de j, (2do 'for')
y deberia ser
saludos!