Merge origin/main into main
This commit is contained in:
commit
1af7beabb7
29
numbers.c
29
numbers.c
@ -11,23 +11,22 @@
|
|||||||
* Duplizieren eines zufälligen Eintrags im Array.
|
* Duplizieren eines zufälligen Eintrags im Array.
|
||||||
* 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. */
|
||||||
|
|
||||||
// Comparison function for unsigned integers used in the binary search tree
|
// Comparison function for unsigned integers used in BST and sorting operations.
|
||||||
// Returns: <0 if a < b, 0 if a == b, >0 if a > b
|
// Returns: <0 if a < b, 0 if a == b, >0 if a > b
|
||||||
// This function is passed to addToTree to determine ordering
|
// This is an internal function (static) since it's not part of the public API.
|
||||||
int compareUnsignedInts(const void *a, const void *b)
|
static int compareUnsignedInts(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
unsigned int valA = *(const unsigned int *)a;
|
unsigned int valA = *(const unsigned int *)a;
|
||||||
unsigned int valB = *(const unsigned int *)b;
|
unsigned int valB = *(const unsigned int *)b;
|
||||||
|
|
||||||
// Simple subtraction works for unsigned integers in this case
|
// Safe comparison without overflow concerns for our range
|
||||||
// (Note: in general subtraction can overflow, but our range is small)
|
|
||||||
if (valA < valB) return -1;
|
if (valA < valB) return -1;
|
||||||
if (valA > valB) return 1;
|
if (valA > valB) return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
// Returns NULL on errors. Use the implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
//
|
//
|
||||||
// Algorithm:
|
// Algorithm:
|
||||||
@ -66,7 +65,7 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
int isDuplicate = 0;
|
int isDuplicate = 0;
|
||||||
usedNumbers = addToTree(usedNumbers, &randomNumber, sizeof(unsigned int), compareUnsignedInts, &isDuplicate);
|
usedNumbers = addToTree(usedNumbers, &randomNumber, sizeof(unsigned int), compareUnsignedInts, &isDuplicate);
|
||||||
|
|
||||||
if (usedNumbers == NULL)
|
if (usedNumbers == NULL) // checks if addTOTree failed
|
||||||
{
|
{
|
||||||
free(numbers);
|
free(numbers);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -103,17 +102,7 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
return numbers;
|
return numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comparison function for qsort to sort unsigned integers in ascending order
|
|
||||||
// Returns: <0 if a < b, 0 if a == b, >0 if a > b
|
|
||||||
static int compareUnsigned(const void *a, const void *b)
|
|
||||||
{
|
|
||||||
unsigned int valA = *(const unsigned int *)a;
|
|
||||||
unsigned int valB = *(const unsigned int *)b;
|
|
||||||
|
|
||||||
if (valA < valB) return -1;
|
|
||||||
if (valA > valB) return 1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||||
//
|
//
|
||||||
@ -140,8 +129,8 @@ unsigned int getDuplicate(const unsigned int *numbers, unsigned int len)
|
|||||||
// Step 3: Copy the array
|
// Step 3: Copy the array
|
||||||
memcpy(sorted, numbers, len * sizeof(unsigned int));
|
memcpy(sorted, numbers, len * sizeof(unsigned int));
|
||||||
|
|
||||||
// Step 4: Sort the copy
|
// Step 4: Sort the copy using the shared comparison function
|
||||||
qsort(sorted, len, sizeof(unsigned int), compareUnsigned);
|
qsort(sorted, len, sizeof(unsigned int), compareUnsignedInts);
|
||||||
|
|
||||||
// Step 5: Compare adjacent elements to find the duplicate
|
// Step 5: Compare adjacent elements to find the duplicate
|
||||||
for (unsigned int i = 0; i < len - 1; i++)
|
for (unsigned int i = 0; i < len - 1; i++)
|
||||||
|
|||||||
BIN
runTest_numbers
BIN
runTest_numbers
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user