Compare commits

..
5 Commits
Author SHA1 Message Date
paulusja 4fc992890a Add solutions for searching. 2026-06-18 13:30:37 +02:00
paulusja 3ed0108d81 Add solution for bubblesort and qsort. 2026-06-18 12:51:33 +02:00
paulusja 556c08db20 Use unsigned. 2026-06-18 09:43:11 +02:00
paulusja 67ae1ba1ca Add previous solution for selection sort. 2026-06-18 09:24:12 +02:00
paulusja cbe48ba34f Use unsigned. 2026-06-18 09:23:11 +02:00
7 changed files with 120 additions and 10 deletions

No files matched your search

+43
View File
@@ -6,3 +6,46 @@ void tausche(int *zahl1, int *zahl2)
*zahl1 = *zahl2;
*zahl2 = tmp;
}
unsigned int findeMaxIdx(int array[], unsigned int len)
{
unsigned int maxIdx = 0;
for(int i = 1; i < len; i++)
{
if(array[i] > array[maxIdx])
{
maxIdx = i;
}
}
return maxIdx;
}
void selectionsort(int array[], unsigned int len)
{
for(int unsortierteLaenge = len; unsortierteLaenge >= 2; unsortierteLaenge--)
{
unsigned int maxIdx = findeMaxIdx(array, unsortierteLaenge);
tausche(&array[maxIdx], &array[unsortierteLaenge-1]);
}
}
void bubblesort(int array[], unsigned int anzahl)
{
int wurdeGetauscht = 1;
for(int i = anzahl-1; i >= 1 && wurdeGetauscht; i--)
{
wurdeGetauscht = 0;
for(int j = 0; j < i; j++)
{
if(array[j] > array[j+1])
{
tausche(&array[j], &array[j+1]);
wurdeGetauscht = 1;
}
}
}
}
+4 -3
View File
@@ -2,8 +2,9 @@
#define SORTIERUNG_H
void tausche(int *zahl1, int *zahl2);
void selectionsort(int array[], int anzahl);
void insertionsort(int array[], int anzahl);
void bubblesort(int array[], int anzahl);
void selectionsort(int array[], unsigned int anzahl);
void insertionsort(int array[], unsigned int anzahl);
void bubblesort(int array[], unsigned int anzahl);
unsigned int findeMaxIdx(int array[], unsigned int len);
#endif
+2 -1
View File
@@ -46,7 +46,8 @@ int main()
zahlen[i] = gibZahlEin();
}
selectionsort(zahlen, anzahl);
//selectionsort(zahlen, anzahl);
bubblesort(zahlen, anzahl);
printf("Sortierte Zahlen:\n");
for(int i = 0; i < anzahl; i++)
+8 -1
View File
@@ -31,6 +31,11 @@ void gibArrayAus(int array[], int anzahl)
printf("\n");
}
int vergleiche(const void *arg1, const void *arg2)
{
return *(const int *)arg1 - *(const int *)arg2;
}
int main()
{
int zufallszahlen[ANZAHL];
@@ -42,7 +47,9 @@ int main()
gibArrayAus(zufallszahlen, ANZAHL);
starteUhr();
selectionsort(zufallszahlen, ANZAHL);
//selectionsort(zufallszahlen, ANZAHL);
//bubblesort(zufallszahlen, ANZAHL);
qsort(zufallszahlen, ANZAHL, sizeof(zufallszahlen[0]), vergleiche);
printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek());
+14 -2
View File
@@ -5,10 +5,22 @@
int *binsucheZgr(int suchElem, int *startZgr, int *endZgr)
{
// TODO
while(startZgr <= endZgr)
{
int *mitte = startZgr + (endZgr - startZgr) / 2;
if(suchElem < *mitte)
endZgr = mitte - 1;
else if(suchElem > *mitte)
startZgr = mitte + 1;
else
return mitte;
}
return NULL;
}
int *binsuche(int suchElem, int array[], int anzahl)
int *binsuche(int suchElem, int array[], unsigned int anzahl)
{
return binsucheZgr(suchElem, array, array + anzahl - 1);
}
+1 -1
View File
@@ -1,6 +1,6 @@
#ifndef SUCHE_H
#define SUCHE_H
int *binsuche(int suchElem, int array[], int anzahl);
int *binsuche(int suchElem, int array[], unsigned int anzahl);
#endif
+48 -2
View File
@@ -42,7 +42,7 @@ int main()
srand(0);
fuelleArray(zufallszahlen, ANZAHL, MIN, MAX);
// TODO: Sortierung mit qsort
qsort(zufallszahlen, ANZAHL, sizeof(zufallszahlen[0]), vergleiche);
gibArrayAus(zufallszahlen, MAX_100(ANZAHL));
@@ -60,7 +60,7 @@ int main()
anzahlVorkommen = zaehleVorkommenBs(suchZahl, zufallszahlen, ANZAHL);
printf("Die Zahl %d ist %dx enthalten. (%lf Sekunden)\n", suchZahl, anzahlVorkommen, messeZeitInSek());
return 0;
}
@@ -74,3 +74,49 @@ void gibArrayAus(int array[], int anzahl)
printf("\n");
}
int vergleiche(const void *arg1, const void *arg2)
{
return *(const int *)arg1 - *(const int *)arg2;
}
int zaehleVorkommenNaiv(int suchElem, int array[], int anzahl)
{
int vorkommen = 0;
for(int i = 0; i < anzahl && array[i] <= suchElem; i++)
{
if(array[i] == suchElem)
vorkommen++;
}
return vorkommen;
}
int zaehleVorkommenBs(int suchElem, int array[], int anzahl)
{
int vorkommen = 0;
int *pos = binsuche(suchElem, array, anzahl);
if(pos != NULL)
{
int *start = pos;
int *ende = array + anzahl - 1;
while(*start == suchElem && start <= ende)
{
vorkommen++;
start++;
}
start = pos-1;
while(*start == suchElem && start >= array)
{
vorkommen++;
start--;
}
}
return vorkommen;
}