mergesort

Weird output of merge sort for no. of entries not being a power of 2. - c

两盒软妹~` 提交于 2019-12-11 10:48:13
问题 I have written a code for mergesort to sort an array of structures in c it works fine when i give no. of entries as a power of 2 but does not even sort the records for other no. of entries. I Would like to learn where is my logic going wrong?? Here is my code: void Mergesort(struct record r[],int n) { int k; if(n>1) { int i,j; struct record r1[n/2]; struct record r2[n-n/2]; for(i=0,j=n/2;i<n/2 && j<n;i++,j++) { r1[i]=r[i]; r2[i]=r[j]; } Mergesort(r1,n/2); Mergesort(r2,n/2); r=Merge(r1,r2,r,n)

Scheme Vector using merge sorting

白昼怎懂夜的黑 提交于 2019-12-11 10:06:31
问题 I have a vector, the elements of each vector is a list, I want to sort the elements regarding to the length of list. I am using this to sort my vector but I got the error (define vector-merge! (lambda (newvec vec left group-size vec-size) (let* ((top-left (min vec-size (+ left group-size))) (right top-left) (top-right (min vec-size (+ right group-size)))) (let mergeloop ((left left) (right right) (i left)) (cond ((and (< left top-left) (< right top-right)) (if (< (vector-ref vec left) (vector

Why does value restriction happen with MergeSort function?

落爺英雄遲暮 提交于 2019-12-11 09:32:07
问题 I have a very simple MergeSort implementation on List. /// Divide the list into (almost) equal halves let rec split = function | [] -> [], [] | [x] -> [x], [] | x1::x2::xs -> let xs1, xs2 = split xs x1::xs1, x2::xs2 /// Merge two sorted lists let rec merge xs ys = match xs, ys with | [], _ -> ys | _, [] -> xs | x::xs', y::ys' when x <= y -> x::merge xs' ys | _, y::ys' -> y::merge xs ys' let rec mergeSort = function | [] -> [] | xs -> let xs1, xs2 = split xs merge (mergeSort xs1) (mergeSort

Javascript merge sort visualisation

↘锁芯ラ 提交于 2019-12-11 07:38:19
问题 I have managed to get a merge sort working in p5.js to sort different length lines but can not figure out how to actually show them being sorted. I.e show them unsorted and then update their position as they are being sorted. I'm not sure if there is an easy way to do this with the way my code is currently written or if I need to break the sorting function up and re draw it after each stage? var values = []; var numLines = 500; function setup() { createCanvas(900, 600); colorMode(HSB, height)

Mergesort - Segmentation fault

扶醉桌前 提交于 2019-12-11 06:09:54
问题 Code directory structure, ./Computing$ ls -LR .: list file.txt mergeSort.c program.exe type.h ./list: arrayImpl.c linkedListImpl.c list.h Compilation procedure: $./Computing gcc -Wall -Werror -DARRAY -I. mergeSort.c ./list/*.c -o program Here is the complete code having files, mergeSort.c , list/* , type.h With the given representation of List , typedef struct List{ void **array; /* Housekeeping - Array enhancement/shrink */ int lastItemPosition; int size; }List; mergesort is performed below

How to “pause” during merge sort to visualize JS p5js

南笙酒味 提交于 2019-12-11 06:02:05
问题 Im working on a sorting visualizer using p5.js, and I need to know if its possible to slow down merge sort so it can be drawn slower. Im currently trying to use the sleep function below to slow down they merge function, but I get Uncaught TypeError: a.slice is not a function. Am I just making a silly mistake, or am I approaching the problem incorrectly? let rectWidth; let depth = 0; function setup() { let numOfRects = document.getElementById('numOfRects').value; let width = document

Most efficient sorting algorithm for sorted sub-sequences

你离开我真会死。 提交于 2019-12-11 04:57:33
问题 I have several sorted sequences of numbers of type long (ascending order) and want to generate one master sequence that contains all elements in the same order. I look for the most efficient sorting algorithm to solve this problem. I target C#, .Net 4.0 and thus also welcome ideas targeting parallelism. Here is an example: s1 = 1,2,3,5,7,13 s2 = 2,3,6 s3 = 4,5,6,7,8 resulting Sequence = 1,2,2,3,3,4,5,5,6,6,7,7,8,13 Edit: When there are two (or more) identical values then the order of those

How do I solve this StackOverflowError?

二次信任 提交于 2019-12-11 02:35:26
问题 In this section of my MergeSort program, I am recursively dividing a unsorted array called "arr". To do this I create two subarrays, "leftArr" and "rightArr", then I fill "leftArr" and "rightArr" with the first half of "arr" and the second half of "arr" respectively. Afterwards I will use recursion to divde / sort leftArr and rightArr. Just wanted clarify: mid = arr.length; To initialise the rightArr I do the following: double halfLength = arr.length * 0.5; if((!(halfLength < 0)) && (!(0 <

Iterative mergesort in C++

我的梦境 提交于 2019-12-11 01:28:21
问题 i'm currently working on iterative version of mergesort, but i've encountered a problem. The program crashes when there are specific sizes of array's like 34,35,36 or 100(just few examples) and it works for the rest(fe works for powers of 2). I've ran some tests and debugged it, and the problem seems to be with the ranges of my iterations/left,right halves of mergesort, but i can't find it. I'll be thankful for your help. Code: int * loadArray(int size){ //i input size of array and it assigns

Javascript implementation of the inversion-counting with merge-sort algorithm

徘徊边缘 提交于 2019-12-10 23:09:08
问题 i am trying to implement the inversion-counting using merge sort algorithm in javascript. I found description and pseudo-code on this site. My implementation looks like this: var mergeAndCount, sortAndCount; /* the merging routine @param List1 the first list to be merged @param List2 the second list to be merged */ mergeAndCount = function(List1, List2) { var count, outputList; outputList = []; count = 0; while (List1.length > 0 || List2.length > 0) { outputList.push(Math.min(List1[0], List2