mergesort

No speedup with naive merge sort parallelization in Haskell

徘徊边缘 提交于 2019-12-21 04:20:54
问题 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,

Merge sorting a struct

我的梦境 提交于 2019-12-20 07:23:27
问题 #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

Counting Inversions Using Merge Sort [closed]

依然范特西╮ 提交于 2019-12-20 03:51:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I have made a merge sort program in Python and it is running perfectly but I have modified it to count the number of inversions involved and now it is giving me an error : Here's my code: def merge_list(left,right,c): result=[] i,j=0,0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append

How to implement merge sort from “The Introduction to Algorithms” by Cormen and Co

一个人想着一个人 提交于 2019-12-19 13:39:14
问题 I'm learning algorithms from Cormen and Co. and I have problem with implementation of merge sort from their pseudocode. I compiled it by: $ gcc -Wall -g merge_sort.c I have a problem because for numbers: 2 4 5 7 1 2 3 6 The result is: 1 2 2 3 3 4 5 5 I tried to read carefully the pseudo code but this does not help me. I want to know what I'm doing wrong. Below is my code: #include <stdio.h> #define SIZE 8 void merge(int *array_of_integers, int p, int q, int r) { int n1 = q - p + 1; int n2 = r

Why is my MergeSort so slow in Python?

人盡茶涼 提交于 2019-12-19 10:03:13
问题 I'm having some troubles understanding this behaviour. I'm measuring the execution time with the timeit-module and get the following results for 10000 cycles: Merge : 1.22722930395 Bubble: 0.810706578175 Select: 0.469924766812 This is my code for MergeSort: def mergeSort(array): if len(array) <= 1: return array else: left = array[:len(array)/2] right = array[len(array)/2:] return merge(mergeSort(left),mergeSort(right)) def merge(array1,array2): merged_array=[] while len(array1) > 0 or len

Why is my MergeSort so slow in Python?

拜拜、爱过 提交于 2019-12-19 10:03:04
问题 I'm having some troubles understanding this behaviour. I'm measuring the execution time with the timeit-module and get the following results for 10000 cycles: Merge : 1.22722930395 Bubble: 0.810706578175 Select: 0.469924766812 This is my code for MergeSort: def mergeSort(array): if len(array) <= 1: return array else: left = array[:len(array)/2] right = array[len(array)/2:] return merge(mergeSort(left),mergeSort(right)) def merge(array1,array2): merged_array=[] while len(array1) > 0 or len

Understanding the Recursion of mergesort

只愿长相守 提交于 2019-12-18 10:36:09
问题 Most of the mergesort implementations I see are similar to this. intro to algorithms book along with online implentations I search for. My recursion chops don't go much further than messing with Fibonacci generation (which was simple enough) so maybe it's the multiple recursions blowing my mind, but I can't even step through the code and understand whats going on even before I even hit the merge function. How is it stepping through this? Is there some strategy or reading I should undergo to

Mergesort implementation is slow

守給你的承諾、 提交于 2019-12-18 09:20:05
问题 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

delphi mergesort for string arrays [closed]

ε祈祈猫儿з 提交于 2019-12-18 07:24:21
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . 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

A Python Segmentation Fault?

心已入冬 提交于 2019-12-18 04:03:11
问题 This generates a Segmentation Fault: 11 and I have no clue why. Before I get into it, here's the code: import numpy.random as nprnd import heapq import sys sys.setrecursionlimit(10**6) def rlist(size, limit_low, limit_high): for _ in xrange(size): yield nprnd.randint(limit_low, limit_high) def iterator_mergesort(iterator, size): return heapq.merge( iterator_mergesort( (iterator.__next__ for _ in xrange(size/2)), size/2), iterator_mergesort( iterator, size - (size/2)) ) def test(): size = 10*