Maximum number of comparisons required to merge two sorted files

In summary, the maximum number of comparisons required to merge two sorted files of length 'm' and 'n' is m+n-1. This can be proven by considering the worst case scenario where one list has all elements smaller than the other list, and then showing that after m+n-2 comparisons, one element remains in each list, and the remaining element is moved without any comparisons, resulting in a total of m+n-1 comparisons.
  • #1
22990atinesh
143
1

Homework Statement


Maximum no. of comparisons required to merge two sorted files of length 'm' and 'n' is..

Homework Equations

The Attempt at a Solution


I think m+n-1
 
Physics news on Phys.org
  • #2
22990atinesh said:
I think m+n-1
Please also provide the reasoning behind your attempt.
 
  • #3
@Orodruin Please see this question
Capture2.jpg


Suppose we have two list LIST-1 & LIST-2 of sorted integers of size 3, 2 respectively (say)

1. First we compare ##1^{st}## element of the both list and suppose we get minimum value from LIST-1, So we store it in the new array. Now we are having two lists of size 2, 2 (1 Comparison)
2. Now we compare ##2^{nd}## element of LIST-1 with ##1^{st}## element of LIST-2 this time element from LIST-2 becomes minimum, So we store it . Now we are having two list of size 2, 1 (1 Comparison)
3. Now we compare ##2^{nd}## element of LIST-1 with ##2^{nd}## element of LIST-2 this time element from LIST-1 becomes minimum, So we store it . Now we are having two list of size 1, 1 (1 Comparison)
4. Now we compare ##3^{rd}## element of LIST-1 with ##2^{nd}## element of LIST-2 this time element from LIST-2 becomes minimum. so we store it . Now we are having two list of size 1, 0 (1 Comparison)
5. Now we store last element left from LIST-1 to the array. Hence 3+2-1 = 4 comparisons
 
  • #4
An alternative, list-m and list-n. Assume all but the last element of list-m is smaller than the first element of list-n, that's m-1 comparisons (not including the compare with last element of list-m yet). Assume last element of list-m is greater than all of the elements in list-n, that's n comparisons, and the last element of list-m is copied to the output list, 0 comparisons. The issue is proving there isn't a pattern that produces more compares.
 
  • #5
rcgldr said:
An alternative, list-m and list-n. Assume all but the last element of list-m is smaller than the first element of list-n, that's m-1 comparisons (not including the compare with last element of list-m yet). Assume last element of list-m is greater than all of the elements in list-n, that's n comparisons, and the last element of list-m is copied to the output list, 0 comparisons. The issue is proving there isn't a pattern that produces more compares.
What I'm missing here
22990atinesh said:
@Orodruin Please see this question
Suppose we have two list LIST-1 & LIST-2 of sorted integers of size 3, 2 respectively (say)

1. First we compare ##1^{st}## element of the both list and suppose we get minimum value from LIST-1, So we store it in the new array. Now we are having two lists of size 2, 2 (1 Comparison)
2. Now we compare ##2^{nd}## element of LIST-1 with ##1^{st}## element of LIST-2 this time element from LIST-2 becomes minimum, So we store it . Now we are having two list of size 2, 1 (1 Comparison)
3. Now we compare ##2^{nd}## element of LIST-1 with ##2^{nd}## element of LIST-2 this time element from LIST-1 becomes minimum, So we store it . Now we are having two list of size 1, 1 (1 Comparison)
4. Now we compare ##3^{rd}## element of LIST-1 with ##2^{nd}## element of LIST-2 this time element from LIST-2 becomes minimum. so we store it . Now we are having two list of size 1, 0 (1 Comparison)
5. Now we store last element left from LIST-1 to the array. Hence 3+2-1 = 4 comparisons
 
  • #6
You're not missing anything. The issue here is how do you prove the worst case number of comparasons without going through every possible scenario?
 
  • #7
rcgldr said:
An alternative, list-m and list-n. Assume all but the last element of list-m is smaller than the first element of list-n, that's m-1 comparisons (not including the compare with last element of list-m yet). Assume last element of list-m is greater than all of the elements in list-n, that's n comparisons, and the last element of list-m is copied to the output list, 0 comparisons. The issue is proving there isn't a pattern that produces more compares.

rcgldr said:
You're not missing anything. The issue here is how do you prove the worst case number of comparasons without going through every possible scenario?

Your above approach also ends up with ##m+n-1## comparisons.
 
  • #8
A logical explanation rather than specific cases would be a better "proof". After each compare, one element is moved from either list-m or list-n. In the worst case scenario, there is 1 element remaining in list-m and 1 element remaining in list-n, and m+n-2 compares have been performed. There is one more compare performed, which moves an element from either list-m or list-n, emptying that list, so the remaining element from the other list is moved without doing any compares, and the worst case scenario is m+n-1 compares.
 
  • Like
Likes 22990atinesh

1. What is the significance of finding the maximum number of comparisons required to merge two sorted files?

The maximum number of comparisons required to merge two sorted files is an important measure of efficiency in sorting algorithms. It helps determine the worst-case scenario for a given algorithm and can be used to compare the performance of different sorting techniques.

2. How is the maximum number of comparisons calculated for merging two sorted files?

The maximum number of comparisons is calculated by considering the worst-case scenario for merging two sorted files. This involves comparing each element from one file to every element in the other file, resulting in a total of m x n comparisons, where m and n are the sizes of the two files.

3. Can the maximum number of comparisons be reduced in any way?

Yes, the maximum number of comparisons can be reduced by using more efficient sorting algorithms such as merge sort or quicksort. These algorithms have a lower worst-case scenario for merging two sorted files, resulting in fewer comparisons.

4. How does the size of the files affect the maximum number of comparisons required?

The size of the files directly affects the maximum number of comparisons required. As the size of the files increases, the number of comparisons also increases. This is because there are more elements to compare in each file, resulting in a higher number of total comparisons.

5. Is the maximum number of comparisons the only factor to consider when evaluating the performance of a sorting algorithm?

No, the maximum number of comparisons is just one aspect to consider when evaluating the performance of a sorting algorithm. Other factors such as time complexity and space complexity also play a crucial role in determining the efficiency of an algorithm.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
4K
Back
Top