Skip to content

Commit b0aa080

Browse files
committed
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.
1 parent d43e072 commit b0aa080

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

qtest.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,23 @@ bool do_sort(int argc, char *argv[])
600600
error_check();
601601

602602
set_noallocate_mode(true);
603+
604+
/* If the number of elements is too large, it may take a long time to check the
605+
* stability of the sort. So, MAX_NODES is used to limit the number of elements
606+
* to check the stability of the sort. */
607+
#define MAX_NODES 100000
608+
struct list_head *nodes[MAX_NODES];
609+
unsigned no = 0;
610+
if (current && current->size && current->size <= MAX_NODES) {
611+
element_t *entry;
612+
list_for_each_entry (entry, current->q, list)
613+
nodes[no++] = &entry->list;
614+
} else if (current && current->size > MAX_NODES)
615+
report(1,
616+
"Warning: Skip checking the stability of the sort because the "
617+
"number of elements %d is too large, exceeds the limit %d.",
618+
current->size, MAX_NODES);
619+
603620
if (current && exception_setup(true))
604621
q_sort(current->q, descend);
605622
exception_cancel();
@@ -624,8 +641,32 @@ bool do_sort(int argc, char *argv[])
624641
ok = false;
625642
break;
626643
}
644+
/* Ensure the stability of the sort */
645+
if (current->size <= MAX_NODES &&
646+
!strcmp(item->value, next_item->value)) {
647+
bool unstable = false;
648+
for (unsigned i = 0; i < MAX_NODES; i++) {
649+
if (nodes[i] == cur_l->next) {
650+
unstable = true;
651+
break;
652+
}
653+
if (nodes[i] == cur_l) {
654+
break;
655+
}
656+
}
657+
if (unstable) {
658+
report(
659+
1,
660+
"ERROR: Not stable sort. The duplicate strings \"%s\" "
661+
"are not in the same order.",
662+
item->value);
663+
ok = false;
664+
break;
665+
}
666+
}
627667
}
628668
}
669+
#undef MAX_NODES
629670

630671
q_show(3);
631672
return ok && !error_check();

0 commit comments

Comments
 (0)