fixing error in dublicateRandomEntry

This commit is contained in:
Jonas Urban 2025-11-23 17:29:45 +01:00
parent e4a7f9ac28
commit 0a30e04157

View File

@ -12,11 +12,24 @@
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */ * in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
static void dublicateRandomEntry (unsigned int *numbers, unsigned int len) static void dublicateRandomEntry (unsigned int *numbers, unsigned int len)
{
if (numbers && len > 2)
{ {
unsigned int dubIndx = rand() % len; unsigned int dubIndx = rand() % len;
unsigned int copyIndx = rand() % len; unsigned int copyIndx;
numbers[copyIndx] = numbers[dubIndx]; do {
copyIndx = rand() % len;
} while (copyIndx == dubIndx);
numbers[len - 1] = numbers[dubIndx];
//switching last entry with random other entry
unsigned int temp;
temp = numbers[copyIndx];
numbers[copyIndx] = numbers[len - 1];
numbers[len - 1] = temp;
}
} }
static int compare(const void *a, const void *b) static int compare(const void *a, const void *b)