Compare commits
3
Commits
67ae1ba1ca
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4fc992890a | ||
|
|
3ed0108d81 | ||
|
|
556c08db20 |
No files matched your search
@@ -31,3 +31,21 @@ void selectionsort(int array[], unsigned int len)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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++)
|
||||
|
||||
@@ -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
@@ -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
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user