mergesort

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

我与影子孤独终老i 提交于 2019-11-26 13:53:32
问题 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

Multithreaded quicksort or mergesort

早过忘川 提交于 2019-11-26 12:22:23
How can I implement a concurrent quicksort or mergesort algorithm for Java? We've had issues on a 16-(virtual)-cores Mac where only one core (!) was working using the default Java sorting algo and it was, well, not good to see that very fine machine be completely underused. So we wrote our own (I wrote it) and we did indeed gain good speedups (I wrote a multithreaded quicksort and due to its partitioning nature it parallelize very well but I could have written a mergesort too)... But my implementation only scales up to 4 threads, it's proprietary code, and I'd rather use one coming from a

Merge Sort a Linked List

老子叫甜甜 提交于 2019-11-26 11:34:54
I was recently brushing up on some fundamentals and found merge sorting a linked list to be a pretty good challenge. If you have a good implementation then show it off here. jayadev Wonder why it should be big challenge as it is stated here, here is a straightforward implementation in Java with out any "clever tricks". //The main function public static Node merge_sort(Node head) { if(head == null || head.next == null) return head; Node middle = getMiddle(head); //get the middle of the list Node left_head = head; Node right_head = middle.next; middle.next = null; //split the list into two halfs

`std::list<>::sort()` - why the sudden switch to top-down strategy?

↘锁芯ラ 提交于 2019-11-26 11:15:47
问题 I remember that since the beginning of times the most popular approach to implementing std::list<>::sort() was the classic Merge Sort algorithm implemented in bottom-up fashion (see also What makes the gcc std::list sort implementation so fast?). I remember seeing someone aptly refer to this strategy as \"onion chaining\" approach. At least that\'s the way it is in GCC\'s implementation of C++ standard library (see, for example, here). And this is how it was in old Dimkumware\'s STL in MSVC

Why is quicksort better than mergesort?

喜夏-厌秋 提交于 2019-11-26 10:56:51
I was asked this question during an interview. They're both O(nlogn) and yet most people use Quicksort instead of Mergesort. Why is that? Quicksort has O( n 2 ) worst-case runtime and O( n log n ) average case runtime. However, it’s superior to merge sort in many scenarios because many factors influence an algorithm’s runtime, and, when taking them all together, quicksort wins out. In particular, the often-quoted runtime of sorting algorithms refers to the number of comparisons or the number of swaps necessary to perform to sort the data. This is indeed a good measure of performance,

Mergesort with Python

我的未来我决定 提交于 2019-11-26 10:16:13
问题 I couldn\'t find any working Python 3.3 mergesort algorithm codes, so I made one myself. Is there any way to speed it up? It sorts 20,000 numbers in about 0.3-0.5 seconds def msort(x): result = [] if len(x) < 2: return x mid = int(len(x)/2) y = msort(x[:mid]) z = msort(x[mid:]) while (len(y) > 0) or (len(z) > 0): if len(y) > 0 and len(z) > 0: if y[0] > z[0]: result.append(z[0]) z.pop(0) else: result.append(y[0]) y.pop(0) elif len(z) > 0: for i in z: result.append(i) z.pop(0) else: for i in y:

Why does Java&#39;s Arrays.sort method use two different sorting algorithms for different types?

删除回忆录丶 提交于 2019-11-26 06:14:02
问题 Java 6\'s Arrays.sort method uses Quicksort for arrays of primitives and merge sort for arrays of objects. I believe that most of time Quicksort is faster than merge sort and costs less memory. My experiments support that, although both algorithms are O(n log(n)). So why are different algorithms used for different types? 回答1: The most likely reason: quicksort is not stable , i.e. equal entries can change their relative position during the sort; among other things, this means that if you sort

A prepared statement, `WHERE .. IN(..)` query and sorting — with MySQL

不问归期 提交于 2019-11-26 04:26:32
问题 Imagine we have a query: SELECT * FROM somewhere WHERE `id` IN(1,5,18,25) ORDER BY `name`; and an array of IDs to fetch: $ids = array(1,5,18,25) With prepared statements it\'s adviced to prepare one statement and call it multiple times: $stmt = $mysqli->prepare(\'SELECT * FROM somewhere WHERE `id`=?;\'); foreach ($ids as $id){ $stmt->bind_params(\'i\', $id); $stmt->exec(); } But now I\'ll have to sort the results manually. Do I have any nice alternatives? 回答1: you could do it this way: $ids =

How to merge two sorted arrays into a sorted array? [closed]

感情迁移 提交于 2019-11-26 03:15:54
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . This was asked of me in an interview and this is the solution I provided: public static int[] merge(int[] a, int[] b) { int[] answer = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { answer[k] = a[i]; i++; } else {

Merge Sort a Linked List

点点圈 提交于 2019-11-26 02:28:41
问题 I was recently brushing up on some fundamentals and found merge sorting a linked list to be a pretty good challenge. If you have a good implementation then show it off here. 回答1: Wonder why it should be big challenge as it is stated here, here is a straightforward implementation in Java with out any "clever tricks". //The main function public static Node merge_sort(Node head) { if(head == null || head.next == null) return head; Node middle = getMiddle(head); //get the middle of the list Node