Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Leafar77 en 11 Febrero 2015, 15:03 pm



Título: Algoritmo de ordenación.
Publicado por: Leafar77 en 11 Febrero 2015, 15:03 pm
Hola buenas, os comento. Estoy desarrollando un algoritmo muy básico de ordenación que me han pedido para clase, no uno existente, sino uno que se nos ocurra. Se que en este campo ya hay mucho escrito pero me parece que la mejor forma de aprender es intentarlo uno mismo, sin buscar ideas por internet, por lo menos a priori. Asi que sin conocer ningún algoritmo de ordenación, y sin tener en cuenta la eficiencia de mi solución esto es lo que he hecho. El algoritmo funciona "a medias". Se ve a simple vista:

Código
  1. #define BIGGEST 32767
  2.  
  3. void sort(int *non_sorted, int *sorted, int size)
  4. {
  5. //Flags
  6. int *beggining = non_sorted;
  7. int *end = (non_sorted + size);
  8.  
  9. //Pointers
  10. int *current = beggining;
  11.  
  12. //Vars
  13. int n_sorted = 0;
  14. int last_minor_found = BIGGEST;
  15. int last_sorted = 0;
  16.  
  17.  
  18. while(n_sorted < size)
  19. {
  20. //Find minor bigger than last sorted
  21. while(current != end)
  22. {
  23. if(*current < last_minor_found && *current > last_sorted)
  24. {
  25. last_minor_found = *current;
  26. }
  27. current++;
  28. }
  29.  
  30. //Save in sorted array
  31. *sorted = last_minor_found;
  32. sorted++;
  33. n_sorted++;
  34.  
  35. //Reinicialize
  36. last_sorted = last_minor_found;
  37. current = beggining;
  38. last_minor_found = BIGGEST;
  39. }
  40. }
  41.  

Output 1:
Código:
Enter size of array: 10  
Non sorted array:
67  69  29  80  14  61  88  99  4  58  
Sorted array:
4  14  29  61  67  69  80  88  99  32767



El problema viene cuando se repite algun numero.

Aqui se ve mas evidente, ordena hasta el cien, y luego pone el maximo. En vez de poner consecutivamente los repetidos.
Código:
Enter size of array: 200
Non sorted array:
2  21  85  19  70  91  82  93  29  100  76  34  85  81  61  9  31  28  75  76  37  53  42  40  12  74  16  66  88  68  49  41  40  85  60  61  75  93  53  3  92  80  89  29  60  49  89  90  28  64  17  64  16  59  55  27  84  22  44  71  41  44  64  80  81  75  41  55  67  45  10  10  25  50  38  84  50  79  26  77  42  42  92  9  52  46  88  36  68  31  58  8  27  73  40  7  47  80  13  65  76  74  75  100  75  64  36  24  42  13  52  35  6  43  44  58  89  31  45  56  61  54  15  39  27  54  97  25  85  62  90  61  35  16  12  10  79  99  33  73  11  37  7  69  79  50  78  67  80  22  74  93  27  41  31  5  94  80  30  31  41  71  43  75  86  54  36  64  5  21  36  67  57  43  35  35  44  12  54  24  85  79  16  12  19  98  68  65  77  49  95  17  19  89  44  4  42  79  20  98 
Sorted array:
2  3  4  5  6  7  8  9  10  11  12  13  15  16  17  19  20  21  22  24  25  26  27  28  29  30  31  33  34  35  36  37  39  40  41  42  43  44  45  47  49  50  52  53  54  56  57  58  60  61  62  64  65  66  67  68  69  70  71  73  74  75  76  77  78  79  80  81  82  85  86  88  89  90  91  92  93  94  95  98  99  100  100  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767  32767

Gracias de antemano!


Título: Re: Algoritmo de ordenación.
Publicado por: NOIS en 11 Marzo 2015, 17:16 pm
No he probado el código, pero creo que el error está en que siempre evalúas el menor, nunca si es igual, por lo que es incapaz de coger los valores repetidos y actuar con "coherencia", como se puede ver :xD.

Espero que te ayude.

Saludos!