mergesort

Merge Sort a Linked List

I was recently brushing up on some fundamentals and found merge sorting a linked list to be a pretty good challenge. If you have a good implementation then show it off here. ...

Quicksort slower than Mergesort??

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...

What sort does Java Collections.sort(nodes) use?

I think it is MergeSort, which is O(n log n). However, the following output disagrees: -1,0000000099000391,0000000099000427 1,0000000099000427,0000000099000346 5,0000000099000391,0000000099000346 1,0000000099000427,0000000099000345 5,0000000099000391,0000000099000345 1,0000000099000346,0000000099000345 I am sorting a nodelist of 4 no...

How far does recursion execute into your function in C++?

I've written recursive functions with the guidance of a friend who is teaching me C++ (as a first language). However, I don't really understand what is going on. He helped me (and the SO community, as well) write a merge sort function. std::vector<int> mergeSort(std::vector<int> original) //code here to create two vectors, farray and s...

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

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 f...

How to improve this mergesort in scheme ?

Hi, I am a C++ programmer, I wrote this code to see if I can think functionally :) Any hints to improve it ? (define (append listOne listTwo) (cond ((null? listOne) listTwo) (else (cons (car listOne) (append (cdr listOne) listTwo))))) (define (merge listOne listTwo) (cond ((null? listOne) listTwo) ((null? listTwo) l...

merge sort implementation to sort by string length - python

I've implemented what I believe to be a merge sort algorithm in python. I've never programmed in Python before, so I used several resources with commands that seemed foreign to me, to gain a better understanding. However, I've also never implemented merge sort in the first place, so I'm not sure if I've even implemented it correctly....

Merge sort in Haskell

Hi, 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...

Merge sort comparison count

I have an assignment where I must output the number of comparisons that are made with the Merge sort algorithm, against an int list[1000] filled with random values (no duplicates). My main question is, have I placed my comparisons++ count incrementer in the correct place. Here is the code I am using int Sort::MergeSort(int* list, int ...

What's wrong with my merge sort implementation in Perl?

I'm trying to write a merge sorting algorithm in Perl and I've attempted to copy the pseudo code from Wikipedia. So this is what I have: sub sort_by_date { my $self = shift; my $collection = shift; print STDERR "\$collection = "; print STDERR Dumper $collection; if ( @$collection <= 1 ) { return $c...

Debugging mergesort

I need to sort a doubly-linked list. According to the almighty wikipedia, mergesort is the way to go for that. The recursive algorithm works reasonably well, but as I'm writing a general-purpose implementation, performance might be an issue. Porting the iterative version for arrays will kill performance as rescanning the list to divide...

Number of Comparisons using merge sort.

If you have 5 distinct numbers, how many comparisons at most do you need to sort this using merge sort? ...

Non-Recursive Merge Sort

Can someone explain in English how does Non-Recursive merge sort works ? Thanks ...

Java recursion and Merge Sort

Hi y'all, I'm trying to write a simple merge sort program in Java, I'm seeing a lot of red in Eclipse. I'm still a beginner, and don't quite see whats wrong. thanks. -Kyle public class merge{ public static int[] mergeSub(int[] array, int left, int right){ if(left<right) { int mid = (left+right)/2; int[] a = ...

merge sort without extra memory

is there any condition in which merge sort can be done without extra memory my prof said it has and he will give bonus point on that. ...

Merge Sort Java

Hi. I am trying to make a merge sort method, but it keeps on giving the wrong sorts. Where do I have change to make it actually sort the array? What part of the code has to be different? Thank you for your time. public static void mergeSort(int[] array, int left, int lHigh, int right, int rHigh) { int elements = (rHigh - l...

How do I use merge sort to delete duplicates?

I use recursive merge sort for sorting a link list, but during the merge sort I would like to delete duplicates. Anyone has insight in how to accomplish this? I am using C code. ...

help with template mergesort function

error C2065:'temp' : undeclared identifier i know that for "temp" i need to declare the type that the array is like int temp[] but what if i don't know what it is.. it could be int or string or double.. how can i create a temp array not specifying what it should be Here is my code: template<class T> void Mergesort(T& a, int first...

Inferred type appears to detect an infinite loop, but what's really happening?

In Andrew Koenig's An anecdote about ML type inference, the author uses merge sort as a learning exercise for ML and is pleased to find an "incorrect" type inference: Much to my surprise, the compiler reported a type of 'a list -> int list In other words, this sort function accepts a list of any type at all and returns a list ...

why is in place merge sort not stable?

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; ...