mergesort

How external merge sort algorithm works?

坚强是说给别人听的谎言 提交于 2019-11-27 05:00:56
问题 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]

Number of Comparisons in Merge-Sort

只愿长相守 提交于 2019-11-27 04:43:46
问题 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? 回答1: Why to count comparisons There are basically two operations to any sorting

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

我怕爱的太早我们不能终老 提交于 2019-11-27 04:35:02
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 version of standard library, as well as in all versions of MSVC all the way to VS2013. However, the

Non-Recursive Merge Sort

戏子无情 提交于 2019-11-26 19:43:47
问题 Can someone explain in English how does Non-Recursive merge sort works ? Thanks 回答1: 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

Why does Java's Arrays.sort method use two different sorting algorithms for different types?

大憨熊 提交于 2019-11-26 18:17:11
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? Michael Borgwardt 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 an already sorted array, it may not stay unchanged. Since primitive types have no identity

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

时光怂恿深爱的人放手 提交于 2019-11-26 17:22:21
问题 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

How to perform merge sort using LINQ?

不打扰是莪最后的温柔 提交于 2019-11-26 16:56:59
问题 Assume that you have two IEnumerbale objects. How we can merge them (in some condition e.g merge in merge sort ...) and create a unique IEnumerable ? I tried this with Zip , but in Zip the two list sizes should be equal (maybe you didn't get exception but maybe we have some data lost.) In addition, I try it by using Enumerable.Range(...).Select(...) but i didn't get an acceptable result. Furthermore, my question is totally different from using Union or this one, in fact as I said like merge

Mergesort in java

浪尽此生 提交于 2019-11-26 16:28:18
问题 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

Passing an array as an argument in C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 16:05:19
问题 I'm writing a merge sort function, and right now I am just using a test case array (there is no input - this is static, for now). I don't know how to pass an array as an argument. Here is my code right now: //merge sort first attempt #include <iostream> #include <algorithm> #include <vector> int mergeSort(int[]); int main() { int originalarray[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 }; mergeSort(originalarray[]); } int mergeSort(int[] originalarray) { int num = (sizeof(originalarray) / sizeof(int

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

半腔热情 提交于 2019-11-26 15:28:55
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? you could do it this way: $ids = array(1,5,18,25); // creates a string containing ?,?,? $clause = implode(',', array_fill(0, count($ids), '?'));