mergesort

How external merge sort algorithm works?

谁都会走 提交于 2019-11-28 02:55:47
I'm trying to understand how external merge sort algorithm works (I saw some answers for same question, but didn't find what I need). I'm reading the book "Analysis Of Algorithms" by Jeffrey McConnell and I'm trying to implement the algorithm described there. For example, I have input data: 3,5,1,2,4,6,9,8,7 , and I can load only 4 numbers into memory. My first step is read the input file in 4-number chunks, sort them in memory and write one to file A and next to file B. I got: A:[1,2,3,5][7] B:[4,6,8,9] Now my question how can I merge chunks from these files to the bigger ones if they will

Number of Comparisons in Merge-Sort

喜欢而已 提交于 2019-11-28 00:18:39
I was studying the merge-sort subject that I ran into this concept that the number of comparisons in merge-sort (in the worst-case, and according to Wikipedia ) equals (n ⌈lg n⌉ - 2 ⌈lg n⌉ + 1); in fact it's between (n lg n - n + 1) and (n lg n + n + O(lg n)). The problem is that I cannot figure out what these complexities try to say. I know O(nlogn) is the complexity of merge-sort but the number of comparisons? Why to count comparisons There are basically two operations to any sorting algorithm: comparing data and moving data. In many cases, comparing will be more expensive than moving. Think

Non-Recursive Merge Sort

醉酒当歌 提交于 2019-11-27 19:03:10
Can someone explain in English how does Non-Recursive merge sort works ? Thanks Loop through the elements and make every adjacent group of two sorted by swapping the two when necessary. Now, dealing with groups of two groups (any two, most likely adjacent groups, but you could use the first and last groups) merge them into one group be selecting the lowest valued element from each group repeatedly until all 4 elements are merged into a group of 4. Now, you have nothing but groups of 4 plus a possible remainder. Using a loop around the previous logic, do it all again except this time work in

How do I use merge sort to delete duplicates?

六月ゝ 毕业季﹏ 提交于 2019-11-27 19:02:02
问题 I use recursive merge sort for sorting a link list, but during the merge sort I would like to delete duplicates. Anyone has insight in how to accomplish this? I am using C code. 回答1: In merge sort you take two (or more) already-sorted lists repeatedly apply the following rules: find the lesser/least of the items of the top of each of the input lists, choosing any of the lowest items if there is a tie remove that item from its list add it to your output list To remove duplicates, you simply

'MergeSort Algorithm' - What's the better implementation in JAVA? [closed]

夙愿已清 提交于 2019-11-27 15:57:37
I know quick sort algorithm, but I am concerned with merge sort algorithm only. I found out on internet two types of merge sort algorithm implementation. But when I compare them with insertion algorithm, they seem less efficient and this is not expected for a large number of items. Enter the number of elements you want to sort: 300000 Time spent to executing BubbleSort: 362123 milliseconds Time spent to executing Selection: 108285 milliseconds Time spent to executing Insertion: 18046 milliseconds Time spent to executing MergeSort: 35968 milliseconds Time spent to executing MergeSort2: 35823

Why does Java 6 Arrays#sort(Object[]) change from mergesort to insertionsort for small arrays?

自古美人都是妖i 提交于 2019-11-27 14:31:56
问题 Java 6's mergesort implementation in Arrays.java uses an insertion-sort if the array length is less than some threshold. This value is hard-coded to 7. As the algorithm is recursive, this eventually happens many times for a large array. The canonical merge-sort algorithm does not do this, just using merge-sort all the way down until there is only 1 element in the list. Is this an optimisation? If so, how is it supposed to help? And why 7 ? The insertion sort (even of <=7 things) increases the

Mergesort in java

拜拜、爱过 提交于 2019-11-27 13:44:27
I am new to Java and have tried to implement mergesort in Java. However, even after running the program several times, instead of the desired sorted output, I am getting the same user given input as the output. I would be thankful if someone could help me understand this unexpected behaviour. import java.io.*; import java.util.Arrays; public class MergeSort { public static void main(String[] args) throws IOException{ BufferedReader R = new BufferedReader(new InputStreamReader(System.in)); int arraySize = Integer.parseInt(R.readLine()); int[] inputArray = new int[arraySize]; for (int i = 0; i <

Explanation of Merge Sort for Dummies

给你一囗甜甜゛ 提交于 2019-11-27 09:47:21
问题 I found this code online: def merge(left, right): result = [] i ,j = 0, 0 while i < len(left) and j < len(right): if left[i] <= right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result += left[i:] result += right[j:] return result def mergesort(list): if len(list) < 2: return list middle = len(list) / 2 left = mergesort(list[:middle]) right = mergesort(list[middle:]) return merge(left, right) It works 100% when I run it. I just do not really get how the merge sort

What sort does Java Collections.sort(nodes) use?

余生长醉 提交于 2019-11-27 07:54:14
I think it is MergeSort, which is O(n log n). However, the following output disagrees: -1,0000000099000391,0000000099000427 1,0000000099000427,0000000099000346 5,0000000099000391,0000000099000346 1,0000000099000427,0000000099000345 5,0000000099000391,0000000099000345 1,0000000099000346,0000000099000345 I am sorting a nodelist of 4 nodes by sequence number, and the sort is doing 6 comparisons. I am puzzled because 6 > (4 log(4)). Can someone explain this to me? P.S. It is mergesort, but I still don't understand my results. Thanks for the answers everyone. Thank you Tom for correcting my math. O

Regarding in-place merge in an array

僤鯓⒐⒋嵵緔 提交于 2019-11-27 07:40:10
I came across the following question. Given an array of n elements and an integer k where k < n . Elements { a 0 ... a k } and { a k +1 ... a n } are already sorted. Give an algorithm to sort in O( n ) time and O(1) space. It does not seem to me like it can be done in O( n ) time and O(1) space. The problem really seems to be asking how to do the merge step of mergesort but in-place. If it was possible, wouldn't mergesort be implemented that way? I am unable to convince myself though and need some opinion. deinst This seems to indicate that it is possible to do in O(lg^2 n) space. I cannot see