Skip to content

Commit

Permalink
Check if sorting implementation is stable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yenslife committed Mar 30, 2024
1 parent d43e072 commit b0aa080
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit b0aa080

Please sign in to comment.