Sorting an Array of 2-D Records

In summary, the sorting algorithm used on an array of records can result in the order (4,5), (2,5), (4,3).
  • #1
logical3902490
2
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
  • #2
Welcome to PF.

Sorry, does "stable" have some definition in the context of sorting algorithms? I don't remember it from my software classes.
 
  • #3
No, it just means any normal sorting algorithm
 
  • #4
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 berkeman
  • #5
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 berkeman
  • #6
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.
 
  • #7
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 jim mcnamara

FAQ: Sorting an Array of 2-D Records

What is an array of 2-D records?

An array of 2-D records is a data structure that stores information in rows and columns, similar to a table. Each row represents a record, and each column represents a specific attribute or field of that record.

Why would you need to sort an array of 2-D records?

Sorting an array of 2-D records allows for easier data analysis and retrieval. It can help organize the records in a specific order, such as alphabetical or numerical, making it easier to find and compare specific data.

How do you sort an array of 2-D records?

The most common way to sort an array of 2-D records is by using a sorting algorithm, such as bubble sort, selection sort, or merge sort. These algorithms compare and rearrange the records based on a specific key or attribute.

Can you sort an array of 2-D records in reverse order?

Yes, an array of 2-D records can be sorted in reverse order by changing the comparison criteria in the sorting algorithm. For example, if the original sort was in ascending order, changing it to descending order will sort the records in reverse.

What is the time complexity of sorting an array of 2-D records?

The time complexity of sorting an array of 2-D records depends on the sorting algorithm used. Some algorithms have a time complexity of O(n^2), while others have a time complexity of O(nlogn). It is important to choose the most efficient algorithm based on the size of the array and the specific sorting needs.

Back
Top