Sorting an Array of 2-D Records

  • Thread starter Thread starter logical3902490
  • Start date Start date
  • Tags Tags
    Array Sorting
Click For Summary

Discussion Overview

The discussion revolves around sorting algorithms applied to an array of 2-D records, specifically focusing on how to sort records based on two components, A and B. Participants explore the implications of using stable sorting algorithms and the order of records after multiple sorting operations.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions whether the resulting order of records after sorting by A and then by B would be (4,3), (2,5), (4,5).
  • Another participant suggests that after sorting by A, the order could be (2, 5), (4, 5), (4, 3), which may lead to a different final order after sorting by B.
  • There is a clarification on the definition of "stable" sorting, indicating that it retains the original order of equal elements.
  • A participant emphasizes the need to determine primary and secondary sort fields to achieve the desired sorting outcome.

Areas of Agreement / Disagreement

Participants express differing views on the outcome of the sorting process, with no consensus reached on the final order of records after applying the sorting algorithms.

Contextual Notes

There are unresolved assumptions regarding the definitions and implications of stable sorting, as well as the specific sorting algorithm used. The discussion also reflects confusion due to edits made to the original post.

logical3902490
Messages
2
Reaction score
0
I've just started learning sorting algorithms on arrays of records and wanted to ensure I'm not doing it wrong so I have a basic question.

I am sorting an array of records, where the records have two components, A and B. For example a record (3,2) has an A value of 3 and a B value of 2.

The input array has the following records in this order: (4,5), (2, 5), (4,3)

If I use any stable sorting algorithm to sort the records in increasing A values, and then use the same algorithm to sort the result of the first sort into increasing B values, will the resulting records be in this order:

(4,3) (2,5) (4,5)
 
Technology news on Phys.org
Welcome to PF.

Sorry, does "stable" have some definition in the context of sorting algorithms? I don't remember it from my software classes.
 
No, it just means any normal sorting algorithm
 
logical3902490 said:
The input array has the following records in this order: (4,5), (2, 5), (4,3)

If I use any stable sorting algorithm to sort the records in increasing A values, and then use the same algorithm to sort the result of the first sort into increasing B values, will the resulting records be in this order:

(4,3) (2,5) (4,5)
Maybe not.
After sorting on the A values, you could end up with (2, 5), (4, 5), (4, 3).
After sorting this new list on the B values, the sort routine could swap the first and third tuples, and leave the one in the middle alone, resulting in (4, 3), (4, 5), (2, 5).
 
  • Like
Likes   Reactions: berkeman
Your algorithm must determine which field is the primary sort field and which is secondary. The primary sort field determines the greatest overall sort and the secondary field is a sort within the primary field values.
 
  • Like
Likes   Reactions: berkeman
berkeman said:
Sorry, does "stable" have some definition in the context of sorting algorithms? I don't remember it from my software classes.
Yes, a stable sort keeps entries that compare equal in their original order. But the OP appears to have been edited.
 
pbuk said:
Yes, a stable sort keeps entries that compare equal in their original order. But the OP appears to have been edited.
Ah, thanks for the heads-up. This thread is now locked with the original version of the OP restored.

@logical3902490 -- If you want to ask an updated question, please start a new thread. It is against the PF rules to substantially alter your original post, since it is completely confusing to the flow of the discussion thread. Thank you.
 
  • Like
Likes   Reactions: jim mcnamara

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
20
Views
2K
Replies
10
Views
2K
Replies
12
Views
3K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K