mergesort

Why should Insertion Sort be used after threshold crossover in Merge Sort

旧街凉风 提交于 2019-11-30 04:58:08
问题 I have read everywhere that for divide and conquer sorting algorithms like Merge-Sort and Quicksort , instead of recursing until only a single element is left, it is better to shift to Insertion-Sort when a certain threshold, say 30 elements, is reached. That is fine, but why only Insertion-Sort ? Why not Bubble-Sort or Selection-Sort , both of which has similar O(N^2) performance? Insertion-Sort should come handy only when many elements are pre-sorted (although that advantage should also

How to fix this non-recursive odd-even-merge sort algorithm?

隐身守侯 提交于 2019-11-30 03:16:33
问题 I was searching for non-recursive odd-even-merge sort algorithm and found 2 sources: a book from Sedgewick R. this SO question Both algorithms are identical but false. The resulting sorting network is not an odd-even-merge sort network. Here is an image of the resulting network with 32 inputs. A vertical line between 2 horizontal lines means compare value a[x] with a[y], if greater then swap the values in the array. (source: flylib.com) (clickable) I copied the code from Java to C and

Why is merge sort worst case run time O (n log n)?

蹲街弑〆低调 提交于 2019-11-29 19:58:54
Can someone explain to me in simple English or an easy way to explain it? On a "traditional" merge sort, each pass through the data doubles the size of the sorted subsections. After the first pass, the file will be sorted into sections of length two. After the second pass, length four. Then eight, sixteen, etc. up to the size of the file. It's necessary to keep doubling the size of the sorted sections until there's one section comprising the whole file. It will take lg(N) doublings of the section size to reach the file size, and each pass of the data will take time proportional to the number

why is merge sort preferred over quick sort for sorting linked lists

大兔子大兔子 提交于 2019-11-29 18:59:30
I read the following in a forum : Merge sort is very efficient for immutable datastructures like linked lists and Quick sort is typically faster than merge sort when the data is stored in memory. However, when the data set is huge and is stored on external devices such as a hard drive, merge sort is the clear winner in terms of speed. It minimizes the expensive reads of the external drive and when operating on linked lists, merge sort only requires a small constant amount of auxiliary storage Can someone help me understand the above argument? why is merge sort preferred for sorting huge linked

Mergesort implementation is slow

荒凉一梦 提交于 2019-11-29 15:46:33
I'am doing a report about different sorting algorithms in C++. What baffles me is that my mergesort seems to be slower than heapsort in both of the languages. What I've seen is that heapsort is supposed to be slower. My mergesort sorts an unsorted array with size 100000 at a speed of 19.8 ms meanwhile heapsort sorts it at 9.7 ms. The code for my mergesort function in C++ is as follows: void merge(int *array, int low, int mid, int high) { int i, j, k; int lowLength = mid - low + 1; int highLength = high - mid; int *lowArray = new int[lowLength]; int *highArray = new int[highLength]; for (i = 0;

String sorting using Merge Sort

和自甴很熟 提交于 2019-11-29 14:49:51
问题 What will be the worst complexity for sorting n strings having n characters each? Will it be just n times its avg. case O(n log n) or something else...? 回答1: As @orangeoctopus, using standard ranking algorithm on a collection of n strings of size n will result in O(n^2 * logn) computation. However - note that you can do it in O(n^2) , with variations on radix sort. The simplest way to do it [in my opinion] - is build a trie, and populate it with all your strings. Entering each string is O(n)

Algorithm to merge multiple sorted sequences into one sorted sequence in C++

≯℡__Kan透↙ 提交于 2019-11-29 13:24:26
I am looking for an algorithm to merge multiple sorted sequences, lets say X sorted sequences with n elements, into one sorted sequence in c++ , can you provide some examples? note: I do not want to use any library There are three methods that do the merging :- Suppose you are merging m lists with n elements each Algorithm 1 :- Merge lists two at a time. Use merge sort like merge routine to merge as the lists are sorted. This is very simple to implement without any libraries. But takes time O(m^2*n) which is small enough if m is not large. Algorithm 2:- This is an improvement over 1. where we

Parallel Merge Sort with threads /much/ slower than Seq. Merge Sort. Help

浪尽此生 提交于 2019-11-29 13:20:02
http://pastebin.com/YMS4ehRj ^ This is my implementation of parallel merge sort. Basically what I do is, For every split, the first half is handled by a thread whereas the second half is sequential (i.e.) say we have an array of 9 elements, [0..4] is handled by Thread 1, [0..1] is handled Thread 2, [5..6] is handled by thread 3 (Look at the source code for clarification). Everything else stays the same, like Merging. But the problem is, this runs much slower than merge sort, even slower than normal bubble sort! And I mean for an array of 25000 int's. I'm not sure where the bottleneck is: Is it

delphi mergesort for string arrays [closed]

荒凉一梦 提交于 2019-11-29 13:04:46
Found this coded mergesort on http://www.explainth.at/en/delphi/dsort.shtml (site down but try wayback machine or this site: http://read.pudn.com/downloads192/sourcecode/delphi_control/901147/Sorts.pas__.htm ) but essentially the array defined was not for an array of string. type TSortArray = array[0..8191] of Double; I want to pass an array of string that would possibly eliminate duplicates (this would be Union?) and preserve original order if possible for later resorting it back to original index position minus the duplicates of course (original index) so array can be passed back for further

For inputs of size n, for which values of n does insertion-sort beat merge-sort? [closed]

戏子无情 提交于 2019-11-29 12:15:13
问题 In the book Introduction to Algorithms (Corman), exercise 1.2-2 asks a the following question about comparing implementations of insertion sort and merge sort. For inputs of size n, insertion sort runs in 8n^2 steps while merge sort runs in 64n lg n steps; for which values of n does insertion sort beat merge sort? Although I am interested in the answer, I am more interested in how to find the answer step by step (so that I can repeat the process to compare any two given algorithms if at all