1 1 vote why not merge sort?we don’t swap in merge sort,we just create auxillary arrays and merge them by changing elements in the original array.should we consider that as a swap? Algorithms made-easy-test-series algorithms sorting + – hitesh159 3.9k views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply Show 2 previous comments hitesh159 commented Apr 16, 2019 reply Follow flag this is the pseudocode for merge procedure from clrs book,I think swap(X,Y) moves the contents of X to Y and Y to X ,but here(in Lines 12-17) it just merges the contents of 2 arrays by editing the original array(A[k]=L[i]).why is this counted as swaping ,we have not moved the previous contents of A[k] to L[i]; 0 0 replyShare srestha commented Apr 18, 2019 reply Follow flag @hitesh159 is it merging without swapping? Without swapping we cannot get a sorted order 0 0 replyShare altamash commented Jul 25, 2019 reply Follow flag Yes answer should be selection sort as it is implements the descending priority queue as an unordered array.the number of interchange is always n-1. 0 0 replyShare Please log in or register to add a comment.
1 1 vote For number of swaps in merge sort, see the pictures below. Recursive merge sort is used. SuvasishDutta answered Apr 16, 2019 SuvasishDutta comment Share Follow See 1 comment 1 1 comment reply SuvasishDutta commented Apr 16, 2019 i edited by SuvasishDutta Apr 17, 2019 reply Follow flag Answer will be option b. In worst case, 1. No of swaps in merge sort= n*(n-1)/2 2. No of swaps in quick sort = n*(n-1)/2 3. No of swaps in bubble sort = n*(n-1)/2 4. No of swaps in selection sort= n-1 1 1 replyShare Please log in or register to add a comment.
0 0 votes Just think about for 2 element array .and apply the merge sort .You can clearly see that there is a sawp between the 2 elements. So yes merge sort also performs swap between elements . No of swap in merge sort =O(N^2) PRANAB NANDY answered Apr 17, 2019 PRANAB NANDY comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes In selection sort the number of swaps is always O(n). Why? We find minimum element in unsorted part of array and swap it with first element of unsorted part of array. So for each phase only 1 swap is there. In Insertion Sort in worst case number of swaps is O($n^2$). For merge sort no of swap is $O(N^2)$ smsubham answered Mar 7, 2020 • edited Mar 7, 2020 by smsubham smsubham comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Firstly, they have not asked about the number of comparisons. They are asking about the swaps that will take place in sortings- In quick sort total 0(n^2) swaps take place. i.e initially we have to find element from LHS to R.H.S. which is greater than pivot and then from R.H.S. to L.H.S. for element smaller than pivot. If found then swap and repeat and divide and repeat. In selection sort find the minimum element and then swap with first element. Repeat and swap. Thus 0(n) swaps. In merge sort first n/2 then n/2 then n/2…… total 0(nlogn) swaps in bubble sort 0(n^2) swaps take place. Hence option B is correct rish1602 answered Jul 15, 2021 • edited Jul 15, 2021 by rish1602 rish1602 comment Share Follow See all 3 Comments 3 3 Comments reply rish1602 commented Jul 15, 2021 reply Follow flag corrected...thanks brother:) 0 0 replyShare avadh commented Oct 12, 2021 reply Follow flag in merge sort its n2 or nlogn please let me sir 0 0 replyShare Awe111 commented Oct 13, 2021 reply Follow flag @avadh Merge sort has a time complexity of O(n log n)... It's easier to confirm this by closely inspecting the iterative implementation you've described. The algorithm scans the list log(n) times. In your example, it merges every pair, then every four, then every eight, then all sixteen, bringing it to a total of 4 = log2(16) iterations. Since the list itself is length n, the total time complexity is in the order of n log(n), which is validated by calculating that 64 elements in total are iterated by scanning a list of 16 items 4 times…. The fault in your analysis is incorrectly assuming that every merge is performed with a time complexity of O(n). The time complexity of each merge in a merge sort cannot trivially be represented in terms of n... 0 0 replyShare Please log in or register to add a comment.
0 0 votes I hope this helps: https://stackoverflow.com/questions/40612181/does-mergesort-have-swaps DAWID15 answered Dec 21, 2021 DAWID15 comment Share Follow 0 reply Please log in or register to add a comment.