mergesort

Merge sort for f sharp

孤者浪人 提交于 2019-12-01 07:10:44
问题 This is my code, when I enter a very large number I get stack overflow error does anyone know why? When i enter a very large number i get that error and im not really sure what is causing it, it is only with large numbers small ones work fine..... // // merge two sorted lists into one: // let rec merge L1 L2 = if L1 = [] && L2 = [] then [] else if L1 = [] then L2 else if L2 = [] then L1 else if L1.Head <= L2.Head then L1.Head :: merge L1.Tail L2 else L2.Head :: merge L1 L2.Tail // //

why is in place merge sort not stable?

偶尔善良 提交于 2019-11-30 16:12:34
The implementation below is stable as it used <= instead of < at line marked XXX. This also makes it more efficient. Is there any reason to use < and not <= at this line? /** class for In place MergeSort **/ class MergeSortAlgorithm extends SortAlgorithm { void sort(int a[], int lo0, int hi0) throws Exception { int lo = lo0; int hi = hi0; pause(lo, hi); if (lo >= hi) { return; } int mid = (lo + hi) / 2; /* * Partition the list into two lists and sort them recursively */ sort(a, lo, mid); sort(a, mid + 1, hi); /* * Merge the two sorted lists */ int end_lo = mid; int start_hi = mid + 1; while (

Merge sort to count split inversions in Python

妖精的绣舞 提交于 2019-11-30 15:40:32
问题 I am trying to use mergesort--which I get--to count the number of split inversions in a list (that is, where an element in the first half of the unsorted list should appear after a given element in the second half of the unsorted list; for example [3 2 1 4] would contain the split inversion (3, 1), but not (3, 2) as 3 and 2 are both in the first half). When I get to the final print statement, I am getting the answer I expect--in this case 9--but the return value is all wonky since it's

Merge sort to count split inversions in Python

我是研究僧i 提交于 2019-11-30 14:48:53
I am trying to use mergesort--which I get--to count the number of split inversions in a list (that is, where an element in the first half of the unsorted list should appear after a given element in the second half of the unsorted list; for example [3 2 1 4] would contain the split inversion (3, 1), but not (3, 2) as 3 and 2 are both in the first half). When I get to the final print statement, I am getting the answer I expect--in this case 9--but the return value is all wonky since it's returning the split value through the recursion. I've tried all sorts of combinations of indexing to no avail

dynamically increasing java heap space

柔情痞子 提交于 2019-11-30 13:07:07
I have written a java program that tests the speed of a couple of multi-threading algorithms on different machines with various numbers of processors. On some machines, merge sort* fails because it requires a sizable heap space to work on very large arrays. I can easily change the java heap space myself before running the program, but I feel like a more robust and easy approach would be to do this task from within the program itself. Is there a way to request/achieve more heap space from the virtual machine during the course of a java program? Note: I do understand that I could execute the

String sorting using Merge Sort

有些话、适合烂在心里 提交于 2019-11-30 09:45:59
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...? 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) and you do it n times - total of O(n^2) do a DFS on the trie, each time you encounter the mark for end for

Python class to merge sorted files, how can this be improved?

元气小坏坏 提交于 2019-11-30 09:23:55
Background: I'm cleaning large (cannot be held in memory) tab-delimited files. As I clean the input file, I build up a list in memory; when it gets to 1,000,000 entries (about 1GB in memory) I sort it (using the default key below) and write the list to a file. This class is for putting the sorted files back together. It works on the files I have encountered thus far. My largest case, so far, is merging 66 sorted files. Questions: Are there holes in my logic (where is it fragile)? Have I implemented the merge-sort algorithm correctly? Are there any obvious improvements that could be made?

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

风格不统一 提交于 2019-11-30 09:01:10
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 possible). At first glance, this problem seems similar to something like finding the break even point in

Space requirements of a merge-sort

主宰稳场 提交于 2019-11-30 08:26:39
I'm trying to understand the space requirements for a Mergesort, O(n). I see that time requirements are basically, amount of levels(logn) * merge(n) so that makes (n log n). Now, we are still allocating n per level, in 2 different arrays, left and right. I do understand that the key here is that when the recursive functions return the space gets deallocated, but I'm not seeing it too obvious. Besides, all the info I find, just states space required is O(n) but don't explain it. Any hint? function merge_sort(m) if length(m) ≤ 1 return m var list left, right, result var integer middle = length(m

Java MergeSort - Out Of Memory Error: Java Heap Space

女生的网名这么多〃 提交于 2019-11-30 06:04:26
问题 I'm trying to get some practice with sorting in Java. I'm working on the merge sort now... Eclipse is outputting Out Of Memory Error: Java Heap space , but I'm not sure how to debug that. I feel like my code is okay- any thoughts? import java.util.ArrayList; import java.util.List; public class Sorts { List<Integer> initialList; public Sorts() { initialList = new ArrayList<Integer>(); initialList.add(2); initialList.add(5); initialList.add(9); initialList.add(3); initialList.add(6); System.out