2025-12-18 13:15:31 +01:00

80 lines
1.7 KiB
C

#include "sortierung.h"
#include <string.h>
static void tausche(int *zahl1, int *zahl2)
{
int tmp = *zahl1;
*zahl1 = *zahl2;
*zahl2 = tmp;
}
void selectionSort(int array[], int anzahl)
{
for(int i = 0; i < anzahl - 1; i++)
{
int minIdx = i;
for(int j = i + 1; j < anzahl; j++)
{
if(array[j] < array[minIdx])
minIdx = j;
}
tausche(&array[i], &array[minIdx]);
}
}
void quickSort(int array[], int anzahl)
{
}
static void mische(int links[], int rechts[], int anzahlLinks, int anzahlRechts)
{
// Achtung: VLAs! Normalerweise verboten!
int hilfeLinks[anzahlLinks];
int hilfeRechts[anzahlRechts];
int idxLinks = 0;
int idxRechts = 0;
int *erg = links;
memcpy(hilfeLinks, links, anzahlLinks * sizeof(int));
memcpy(hilfeRechts, rechts, anzahlRechts * sizeof(int));
while(idxLinks < anzahlLinks && idxRechts < anzahlRechts)
{
if(hilfeLinks[idxLinks] <= hilfeRechts[idxRechts])
{
*erg = hilfeLinks[idxLinks];
idxLinks++;
}
else
{
*erg = hilfeRechts[idxRechts];
idxRechts++;
}
erg++;
}
for(; idxLinks < anzahlLinks; idxLinks++)
{
*erg = hilfeLinks[idxLinks];
erg++;
}
for(; idxRechts < anzahlRechts; idxRechts++)
{
*erg = hilfeRechts[idxRechts];
erg++;
}
}
void mergeSort(int array[], int anzahl)
{
if(anzahl > 1)
{
unsigned int mitte = anzahl / 2;
mergeSort(array, mitte);
mergeSort(array+mitte, anzahl - mitte);
mische(array, array+mitte, mitte, anzahl - mitte);
}
}