mergesort

Program crashes when array size is one million [duplicate]

你说的曾经没有我的故事 提交于 2019-12-04 01:43:08
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Big array gives segmentation error in C i'm trying to compare merge sort and quick sort with different input sizes like 10.000, 100.000, and 1.000.000. However, when i give one million input size program is crashing and i don't know why ?.On the other hand, array is filled with reading from a file which contains numbers and here is my simple merge sort. #include <stdio.h> #include <stdlib.h> #include <sys/time.h

No speedup with naive merge sort parallelization in Haskell

孤人 提交于 2019-12-03 13:05:52
Note: This post was completely rewritten 2011-06-10; thanks to Peter for helping me out . Also, please don't be offended if I don't accept one answer, since this question seems to be rather open-ended. (But, if you solve it, you get the check mark, of course). Another user had posted a question about parallelizing a merge sort. I thought I'd write a simple solution, but alas, it is not much faster than the sequential version. Problem statement Merge sort is a divide-and-conquer algorithm, where the leaves of computation can be parallelized. The code works as follows: the list is converted into

when is insertion sort faster than merge sort?

大城市里の小女人 提交于 2019-12-03 11:21:10
问题 For a homework problem, I was told that insertion sort runs at 8n^2 and that merge sort runs at 64(n(lg n)). As part of the solution I was given, it said that insertion sort was faster than merge sort as long as n <= 43, but the teacher's answer doesn't really explain why or how he arrived at 43. Can anyone explain this? I'm not that great with figuring out run times so I'm trying to get a better understanding. And yes, I tried asking the teacher, but I was still confused. Any help would be

Non-recursive merge sort with two nested loops - how?

不问归期 提交于 2019-12-03 09:22:37
问题 First question here, and yes this is a homework question. We are tasked with performing merge sort on an array (which I am familiar with), but in a way I am unsure of how to do. Usually I would have a separate merge and merge sort function, and use the two. However, it sounds like he wants everything in one method? I was just hoping maybe someone could help clear things up for me, or put them into terms I can better understand. From the assignment: you will need to implement a non-recursive

Merge sort in Haskell

泄露秘密 提交于 2019-12-03 06:57:40
问题 I am new to Haskell and I am trying to implement a few known algorithms in it. I have implemented merge sort on strings. I am a bit disappointed with the performance of my Haskell implementation compared to C and Java implementations. On my machine (Ubuntu Linux, 1.8 GHz), C (gcc 4.3.3) sorts 1 000 000 strings in 1.85 s, Java (Java SE 1.6.0_14) in 3.68 s, Haskell (GHC 6.8.2) in 25.89 s. With larger input (10 000 000 strings), C takes 21.81 s, Java takes 59.68 s, Haskell starts swapping and I

Mergesort - Is Bottom-Up faster than Top-Down?

偶尔善良 提交于 2019-12-03 06:22:29
I've been reading "Algorithms, 4th Ed" by Sedgewick & Wayne, and along the way I've been implementing the algorithms discussed in JavaScript. I recently took the mergesort examples provided in the book to compare top-down and bottom-up approaches... but I'm finding that bottom-up is running faster (I think). See my analysis on my blog. - http://www.akawebdesign.com/2012/04/13/javascript-mergesort-top-down-vs-bottom-up/ I have not been able to find any discussion that says one method of mergesort should be faster than the other. Is my implementation (or analysis) flawed? Note: my analysis

Quicksort slower than Mergesort?

自古美人都是妖i 提交于 2019-12-03 04:49:15
I was working on implementing a quicksort yesterday, and then I ran it, expecting a faster runtime than the Mergesort (which I had also implemented). I ran the two, and while the quicksort was faster for smaller data sets <100 elements (and I did verify that it works), the mergesort became the quicker algorithm fairly quickly. I had been taught that quicksort is almost always "quicker" than mergesort, and I understand that there is some debate on this topic, but I at least expected it to be closer than this. For data sets >10000 elements, the mergesort was over 4 times faster. Is this to be

Merge sort in Haskell

只谈情不闲聊 提交于 2019-12-02 21:35:43
I am new to Haskell and I am trying to implement a few known algorithms in it. I have implemented merge sort on strings. I am a bit disappointed with the performance of my Haskell implementation compared to C and Java implementations. On my machine (Ubuntu Linux, 1.8 GHz), C (gcc 4.3.3) sorts 1 000 000 strings in 1.85 s, Java (Java SE 1.6.0_14) in 3.68 s, Haskell (GHC 6.8.2) in 25.89 s. With larger input (10 000 000 strings), C takes 21.81 s, Java takes 59.68 s, Haskell starts swapping and I preferred to stop the program after several minutes. Since I am new to Haskell, I would be interested

Mergesort Getting an Error in F#

断了今生、忘了曾经 提交于 2019-12-02 12:06:08
let rec merge = function | ([], ys) -> ys | (xs, []) -> xs | (x::xs, y::ys) -> if x < y then x :: merge (xs, y::ys) else y :: merge (x::xs, ys) let rec split = function | [] -> ([], []) | [a] -> ([a], []) | a::b::cs -> let (M,N) = split cs (a::M, b::N) let rec mergesort = function | [] -> [] | L -> let (M, N) = split L merge (mergesort M, mergesort N) mergesort [5;3;2;1] // Will throw an error. I took this code from here StackOverflow Question but when I run the mergesort with a list I get an error: stdin(192,1): error FS0030: Value restriction. The value 'it' has been inferred to have generic

Merge sorting a struct

不羁的心 提交于 2019-12-02 07:30:44
#include<stdio.h> #include<stdlib.h> typedef struct points{ float axis[2]; int id; }Points; typedef enum{ SortById, SortByXAxis }SortType; Points* fill_Array(char* filename, int* length); void Print_set(Points* set, int number_of_points); void mergesort(Points* set, int low, int high, int number_of_points,SortType sort); void merge(Points* set, int low, int middle, int high, int number_of_points,SortType sort); int main(int argc, char* argv[]) { int length; Points *array; array=fill_Array(argv[1],&length); Print_set(array,length); printf("\n\n"); mergesort(array,0,length,length,SortById);