Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if sorting implementation is stable #177

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@jserv jserv Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the sliding window technique to track partial nodes instead of using a predefined number of nodes during a customized sorting routine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to use the sliding window technique, how would we ensure the relative order of nodes? Without additional data structures, the information about the relative order of nodes would be lost after sorting. Can the sliding window still be utilized under these circumstances, or have I misunderstood your suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to use the sliding window technique, how would we ensure the relative order of nodes? Without additional data structures, the information about the relative order of nodes would be lost after sorting. Can the sliding window still be utilized under these circumstances, or have I misunderstood your suggestion?

Think of the facility of queue operations. We are ready to allocate the temporary nodes on demand. What I care is the fixed length for checking purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're considering avoiding the fixed length for checking purposes, I'm thinking of trying to use the node's address as the key for hashing. With an additional data structure, we can determine the original order of the queue. This approach can handle a large amount of test data efficiently due to the properties of hashing, with reduced time complexity. However, we'll need to decide on a fixed array length if we're using an array as a hash table. I'm a bit stuck on this point.

截圖 2024-03-27 上午2 49 15

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
Loading