Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define SIZE1 20
- #define SIZE2 20
- void bubblesort(int array[], int size);
- void selectionsort(int Array[], int Size);
- int main(void)
- {
- int array1[SIZE1] = { 11,10,11,19,14,2,18,11,17,20,16,20,16,3,3,6,9,17,16,4 };
- printf("Unsortiertes Array:\n");
- for (int i = 0; i < SIZE1; ++i)
- printf("%3d", array1[i]);
- puts("\nDas Array ist NICHT sortiert!");
- printf("\n\nSortiertes Array (bubble-Sort):\n");
- bubblesort(array1, SIZE1);
- /********************************************************************/
- /********************************************************************/
- /********************************************************************/
- int array2[SIZE2] = { -10,-4,-4,8,3,3,-9,-8,3,7,4,-8, 9, 6, -4, -7, 3, 3, -9, -10 };
- printf("Unsortiertes Array:\n");
- for (int i = 0; i < SIZE1; ++i)
- printf("%3d", array2[i]);
- puts("\nDas Array ist NICHT sortiert!");
- printf("\n\nSortiertes Array (Selction-Sort):\n");
- selectionsort(array2, SIZE2);
- return 0;
- }
- void bubblesort(int array[], int size)
- {
- int i = 0, j = 0, tmp;
- for (i = 1; i < size + 1; i++)
- {
- for (j = 0; j < size - i; j++)
- {
- if (array[j] < array[j + 1]) // Vergleich nachbare Zahlen
- {
- tmp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = tmp;
- } // end if
- } // end innere Schleife
- printf(" %3d", array[j]);
- } // end äußere Schleife
- printf("\nDas Array ist aufsteigend sortiert.\n\n");
- puts("**************************************************************");
- } //end Bubblesort Funktion
- void selectionsort(int Array[], int Size)
- {
- int i, j, tmp, min; // i ist Kontrollvariable für die äußere, j für die innere Schleife.
- // tmp ist Zwischenspeicher beim Tauschen. min merkt sich den kleinsten Wert im Teilarray.
- for (i = 0; i < Size ; i++)
- {
- min = i;
- for (j = i ; j < Size; j++)
- {
- if (Array[j] < Array[min])
- min = j; // teilarray zum Speichern der kleinste Wert
- } //end innere Schleife
- tmp = Array[min]; // Tauschen
- Array[min] = Array[i];
- Array[i] = tmp;
- printf(" %3d", Array[i]);
- } // end äußere Schleife
- printf("\nDas Array ist aufsteigend sortiert.\n\n");
- } //end Selectionsort Funktion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement