From b0aa080e0ee61b745d8b6c30711496fff2f48bce Mon Sep 17 00:00:00 2001 From: yenslife <77geo5rge6@gmail.com> Date: Sun, 31 Mar 2024 03:59:25 +0800 Subject: [PATCH] Check if sorting implementation is stable Introduced node numbering in qtest.c to evaluate the stability of q_sort's sorting algorithm. When the algorithm encounters two nodes with the same value, it searches for the address of the node record in the nodes array. It then compares the found node to the current node (cur). If the found node is the same as the current node, it indicates that these two duplicate nodes have not been swapped in position after sorting. However, if the found node is cur->next, it means that the position of the nodes has been swapped. That is, the sorting implementation is unstable. The performance of the testing code was evaluated by measuring the elapsed time for q_sort's operation on different numbers of nodes with duplicate values. Node counts ranging from 1000 to 500,000 were examined. Specifically, for the 1000-node count, the elapsed time was recorded as 0.0279 seconds, and for the 500,000-node count, it was 61.44 seconds. For the 100,000-node count, the elapsed time was 2.45 seconds. The elapsed time showed a significant increase starting from the 100,000-node count, underscoring potential performance issues with larger datasets. This method relies on auxiliary data structures to track node pointers and their original order, avoiding alterations to the structure in queue.h. However, stability testing is limited to a maximum of 100,000 elements (MAX_NODES) to address potential performance concerns. --- qtest.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/qtest.c b/qtest.c index 44857cf30..8e2217d41 100644 --- a/qtest.c +++ b/qtest.c @@ -600,6 +600,23 @@ bool do_sort(int argc, char *argv[]) error_check(); set_noallocate_mode(true); + +/* If the number of elements is too large, it may take a long time to check the + * stability of the sort. So, MAX_NODES is used to limit the number of elements + * to check the stability of the sort. */ +#define MAX_NODES 100000 + struct list_head *nodes[MAX_NODES]; + unsigned no = 0; + if (current && current->size && current->size <= MAX_NODES) { + element_t *entry; + list_for_each_entry (entry, current->q, list) + nodes[no++] = &entry->list; + } else if (current && current->size > MAX_NODES) + report(1, + "Warning: Skip checking the stability of the sort because the " + "number of elements %d is too large, exceeds the limit %d.", + current->size, MAX_NODES); + if (current && exception_setup(true)) q_sort(current->q, descend); exception_cancel(); @@ -624,8 +641,32 @@ bool do_sort(int argc, char *argv[]) ok = false; break; } + /* Ensure the stability of the sort */ + if (current->size <= MAX_NODES && + !strcmp(item->value, next_item->value)) { + bool unstable = false; + for (unsigned i = 0; i < MAX_NODES; i++) { + if (nodes[i] == cur_l->next) { + unstable = true; + break; + } + if (nodes[i] == cur_l) { + break; + } + } + if (unstable) { + report( + 1, + "ERROR: Not stable sort. The duplicate strings \"%s\" " + "are not in the same order.", + item->value); + ok = false; + break; + } + } } } +#undef MAX_NODES q_show(3); return ok && !error_check();