Compare commits
5
Commits
487f103de4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4fc992890a | ||
|
|
3ed0108d81 | ||
|
|
556c08db20 | ||
|
|
67ae1ba1ca | ||
|
|
cbe48ba34f |
No files matched your search
@@ -6,3 +6,46 @@ void tausche(int *zahl1, int *zahl2)
|
|||||||
*zahl1 = *zahl2;
|
*zahl1 = *zahl2;
|
||||||
*zahl2 = tmp;
|
*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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,9 @@
|
|||||||
#define SORTIERUNG_H
|
#define SORTIERUNG_H
|
||||||
|
|
||||||
void tausche(int *zahl1, int *zahl2);
|
void tausche(int *zahl1, int *zahl2);
|
||||||
void selectionsort(int array[], int anzahl);
|
void selectionsort(int array[], unsigned int anzahl);
|
||||||
void insertionsort(int array[], int anzahl);
|
void insertionsort(int array[], unsigned int anzahl);
|
||||||
void bubblesort(int array[], int anzahl);
|
void bubblesort(int array[], unsigned int anzahl);
|
||||||
|
unsigned int findeMaxIdx(int array[], unsigned int len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -46,7 +46,8 @@ int main()
|
|||||||
zahlen[i] = gibZahlEin();
|
zahlen[i] = gibZahlEin();
|
||||||
}
|
}
|
||||||
|
|
||||||
selectionsort(zahlen, anzahl);
|
//selectionsort(zahlen, anzahl);
|
||||||
|
bubblesort(zahlen, anzahl);
|
||||||
|
|
||||||
printf("Sortierte Zahlen:\n");
|
printf("Sortierte Zahlen:\n");
|
||||||
for(int i = 0; i < anzahl; i++)
|
for(int i = 0; i < anzahl; i++)
|
||||||
|
|||||||
@@ -31,6 +31,11 @@ void gibArrayAus(int array[], int anzahl)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int vergleiche(const void *arg1, const void *arg2)
|
||||||
|
{
|
||||||
|
return *(const int *)arg1 - *(const int *)arg2;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int zufallszahlen[ANZAHL];
|
int zufallszahlen[ANZAHL];
|
||||||
@@ -42,7 +47,9 @@ int main()
|
|||||||
gibArrayAus(zufallszahlen, ANZAHL);
|
gibArrayAus(zufallszahlen, ANZAHL);
|
||||||
|
|
||||||
starteUhr();
|
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());
|
printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek());
|
||||||
|
|
||||||
|
|||||||
+14
-2
@@ -5,10 +5,22 @@
|
|||||||
|
|
||||||
int *binsucheZgr(int suchElem, int *startZgr, int *endZgr)
|
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);
|
return binsucheZgr(suchElem, array, array + anzahl - 1);
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#ifndef SUCHE_H
|
#ifndef SUCHE_H
|
||||||
#define SUCHE_H
|
#define SUCHE_H
|
||||||
|
|
||||||
int *binsuche(int suchElem, int array[], int anzahl);
|
int *binsuche(int suchElem, int array[], unsigned int anzahl);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -42,7 +42,7 @@ int main()
|
|||||||
srand(0);
|
srand(0);
|
||||||
|
|
||||||
fuelleArray(zufallszahlen, ANZAHL, MIN, MAX);
|
fuelleArray(zufallszahlen, ANZAHL, MIN, MAX);
|
||||||
// TODO: Sortierung mit qsort
|
qsort(zufallszahlen, ANZAHL, sizeof(zufallszahlen[0]), vergleiche);
|
||||||
|
|
||||||
gibArrayAus(zufallszahlen, MAX_100(ANZAHL));
|
gibArrayAus(zufallszahlen, MAX_100(ANZAHL));
|
||||||
|
|
||||||
@@ -74,3 +74,49 @@ void gibArrayAus(int array[], int anzahl)
|
|||||||
printf("\n");
|
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