Advertisement
Guest User

4.01

a guest
Dec 9th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE1 20
  4. #define SIZE2 20
  5. void bubblesort(int array[], int size);
  6. void selectionsort(int Array[], int Size);
  7. int main(void)
  8. {
  9. int array1[SIZE1] = { 11,10,11,19,14,2,18,11,17,20,16,20,16,3,3,6,9,17,16,4 };
  10. printf("Unsortiertes Array:\n");
  11. for (int i = 0; i < SIZE1; ++i)
  12. printf("%3d", array1[i]);
  13. puts("\nDas Array ist NICHT sortiert!");
  14.  
  15.  
  16. printf("\n\nSortiertes Array (bubble-Sort):\n");
  17. bubblesort(array1, SIZE1);
  18.  
  19. /********************************************************************/
  20. /********************************************************************/
  21. /********************************************************************/
  22.  
  23. int array2[SIZE2] = { -10,-4,-4,8,3,3,-9,-8,3,7,4,-8, 9, 6, -4, -7, 3, 3, -9, -10 };
  24. printf("Unsortiertes Array:\n");
  25. for (int i = 0; i < SIZE1; ++i)
  26. printf("%3d", array2[i]);
  27. puts("\nDas Array ist NICHT sortiert!");
  28.  
  29.  
  30. printf("\n\nSortiertes Array (Selction-Sort):\n");
  31. selectionsort(array2, SIZE2);
  32.  
  33.  
  34. return 0;
  35. }
  36.  
  37. void bubblesort(int array[], int size)
  38. {
  39. int i = 0, j = 0, tmp;
  40.  
  41. for (i = 1; i < size + 1; i++)
  42. {
  43. for (j = 0; j < size - i; j++)
  44. {
  45. if (array[j] < array[j + 1]) // Vergleich nachbare Zahlen
  46. {
  47. tmp = array[j];
  48. array[j] = array[j + 1];
  49. array[j + 1] = tmp;
  50. } // end if
  51. } // end innere Schleife
  52. printf(" %3d", array[j]);
  53. } // end äußere Schleife
  54. printf("\nDas Array ist aufsteigend sortiert.\n\n");
  55. puts("**************************************************************");
  56. } //end Bubblesort Funktion
  57.  
  58.  
  59. void selectionsort(int Array[], int Size)
  60. {
  61. int i, j, tmp, min; // i ist Kontrollvariable für die äußere, j für die innere Schleife.
  62. // tmp ist Zwischenspeicher beim Tauschen. min merkt sich den kleinsten Wert im Teilarray.
  63. for (i = 0; i < Size ; i++)
  64. {
  65. min = i;
  66. for (j = i ; j < Size; j++)
  67. {
  68. if (Array[j] < Array[min])
  69. min = j; // teilarray zum Speichern der kleinste Wert
  70. } //end innere Schleife
  71.  
  72. tmp = Array[min]; // Tauschen
  73. Array[min] = Array[i];
  74. Array[i] = tmp;
  75. printf(" %3d", Array[i]);
  76. } // end äußere Schleife
  77. printf("\nDas Array ist aufsteigend sortiert.\n\n");
  78. } //end Selectionsort Funktion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement