Questions tagged [mergesort]

Merge sort is an O(n log n) comparison-based sorting algorithm.

Filter by
Sorted by
Tagged with
4
votes
1answer
45 views

Merge Sort from Scratch in Python

I built a merge sort in python just to have a more thorough understanding of how the algorithm works. What I have written works fine, but I assume there is room for improvement and ways to make my ...
3
votes
1answer
55 views

Iterative Merge Sort Algorithm (Python)

Merge Sort Merge Sort algorithm is a general-purpose comparison-based sorting algorithm. Most implementations produce a stable sort, in which the order of equal elements is preserved. Here, our ...
3
votes
1answer
49 views

Recursive Merge Sort Algorithm (Python)

Merge Sort Merge Sort algorithm is a general-purpose comparison-based sorting algorithm. Most implementations produce a stable sort, in which the order of equal elements is preserved. Just for ...
1
vote
2answers
114 views

Merge k sorted lists in Java

I hava a question similiar with mege in Python but I am trying to write Java code. Here is my code: The input is: The first line - a number of arrays (k); Each next line - the first number is the ...
4
votes
1answer
87 views

LeetCode: Merge k sorted lists C#

https://leetcode.com/problems/merge-k-sorted-lists/ Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
4
votes
4answers
93 views

MergeSort in Javascript

I implemented the following mergesort algorithm in JavaScript, but I want to know your opinion about if is possible to do better than this. ...
1
vote
0answers
47 views

Vector of numbers where merge sort is slow

I'm starting to study sorting algorithms in Python. Merge Sort has a complexity at best and worst cases \$\mathcal{O}(n\log{}n)\$, I want generation a list where Merge Sort is slow but I would some ...
5
votes
4answers
194 views

C-based hash matching cracker

How can I improve this hash matching program using either abstraction or search/sort algorithms? We briefly were introduced to searching methods such as bubble sort and merge sort, but I'm not sure ...
1
vote
1answer
48 views

What are the downsides of my Mergesort implementation?

Could I ask you to evaluate my implementation of mergesort? I think that it's consistent with \$\mathcal{O}(n \log n)\$ complexity but has a downside that I create a new ...
4
votes
2answers
74 views

Merge sorted lists

I want to write a function that merges two sorted lists in Python 3. For example: >> merge_lists([2, 5, 9], [1, 6, 8, 10]) [1, 2, 5, 6, 8, 9, 10] My ...
8
votes
0answers
48 views

Parallel merge sort with thread- and bufferpooling

I did wrote this parallel merge sort with no external dependencies. I tried to make use of modern C++ as much as possible. Can you please tell me how it is? ...
3
votes
2answers
138 views

Merge Sort C++11 C++17

Question Any way I can optimize this further using new C++11 or C++17 features? Would also like feedback on my variable naming, memory management, and style (notice my placement of ...
6
votes
2answers
864 views

C++ parallel merge sort

I'm a first year computer engineering student who's learning about algorithms and data structures. I've implemented a parallel merge sort algorithm in C++ and would like constructive criticism. This ...
1
vote
0answers
48 views

Sorts (merge, quick, bubble) in Python

I'm new to python, and I wrote the following code. Please review, critique and enhance. This project compares quicksort, mergesort and bubblesort write/test the partition routine for ...
3
votes
1answer
26 views

Python merge sort for a list containing distinct values

This code below is what I think should be the implementation of merge sort in Python and it works as expected. It sorts the given list, where we can safely assume that all values are distinct. But ...
2
votes
1answer
288 views

External polyphase merge sort

For comparison with other external sorting algorithms, I have implemented external polyphase merge sort, purported to be useful to sort large files that do not fit into main memory. More theoretical ...
3
votes
1answer
131 views

Adaptive Merge Sort in C++

As the title says, I'm trying to implement a merge sort algorithm in C++ that's also adaptive. This is a personal exercise, and I don't have any specific application in mind. My main goal is to ...
3
votes
0answers
275 views

Linked-list natural merge sort in Python

I implemented the linked-list natural merge sort in Python, and I refered to this awesome if not poetic gist and this implementation in Go. Here is my code: ...
2
votes
0answers
188 views

MergeSort linked list implementation

I wrote a MergeSort implementation of doubly linked list, based on the MergeSort implementation of array on the textbook Algorithms 4th, which is definitely \$O(n \log n)\$. I think my linked list ...
4
votes
1answer
235 views

Natural merge: mergesort that uses already sorted subarrays

The code bellow is my implementation for the natural merge exercise in Robert Sedgwick's Algorithms book: Write a version of bottom-up mergesort that takes advantage of order in the array by ...
2
votes
1answer
77 views

Merge sort implementation with various improvements

So I was working on merge sort today, and was trying to speed it up. I would like your opinion on the implementation and if it's possible to make it faster. Besides that I was wondering if there are ...
3
votes
0answers
35 views

Implementation of merge sort that takes sublinear extra space

I implemented an algorithm that takes only sublinear extra space as required in the exercise description: Develop a merge implementation that reduces the extra space requirement to max(M, N/M). (...
2
votes
2answers
89 views

Python merge sort

In Algorithms Fourth Edition by Robert Sedgewick and Kevin Wayne, the exercise 2.2.10 states that: Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order ...
0
votes
1answer
106 views

Merge sort approach in Python

I'm working my way through a book on algorithms. One of the first challenges is the merge sort. I've seen this implemented before, but as the book suggested I coded one from just the method without ...
1
vote
2answers
138 views

Merge Sort code in C

Below is the code of Merge Sort for sorting an integer array in ascending order of values written by me. By looking at the code, I am thinking I can make the code more compact and secure, but not ...
5
votes
3answers
1k views

C++ code to sort a list of elements

This is a code uses mergesort algorithm to sort a list of elements.What are the possible improvements on the code?Also are there any unhealthy coding practice involved in this code? ...
3
votes
1answer
359 views

Merge sort Java implementation for an interview

I just learned Merge sort, and I really like this Merge sort implementation since it is very logical and very easy to follow. If I was asked in an interview to implement merge sort, will this ...
5
votes
2answers
135 views

Move friendly merge sort - follow-up

This is a follow up to this question. I'm only interested to see if this is implemented correctly for move-only types. ...
7
votes
3answers
1k views

Merge Sort implemented using iterators

...
3
votes
2answers
302 views

Merge two sorted lists in Python

Merge two sorted lists ...
2
votes
2answers
250 views

Bottom up merge sort in Haskell

Although this sort works on Int lists < length million, when the stack overflows, the last clause of the merge function definition looks odd to me, recursing in 2 places. Is there clearer way to ...
2
votes
0answers
58 views

Mergesort in Python optimization

I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster ...
5
votes
2answers
159 views

Writing Merge Sort in C++

I am learning programming online and currently watching CS50 lectures, in third lecture, professor introduces us to merge_sort though only through pseudo code, so I wanted someone to review my ...
1
vote
3answers
423 views

Using mergesort to sort lexicographically

The program should (and does) the following (paraphrased assignment): There is a catalogue with a list of motorcycles. They have a - Name (spaceless string of 20 chars) - Price (int < 99 ...
3
votes
2answers
327 views

Mergesort linked list in C++

My teacher gave us an exercise. We have to sort as quickly as possible. The fastest one gets a reward. On my own system, I am sorting 125000 words within 1.2 seconds. On my teacher's system that is 0....
1
vote
1answer
453 views

Recursive merge-sort inversion count algorithm very slow

I am trying to implement a recursive count of inversions in a list of integers by using merge sort and counting the number of split inversions on each recursion. My code works but runs painfully ...
2
votes
1answer
168 views

merge_sort and unit testing

I made a merge sort and wanted to ask for code review. It passed all of the unit testing. The idea behind the classic Mergesort algorithm is to divide an array in half, sort each half, and merge the ...
5
votes
1answer
422 views

Multi-threaded in-place Mergesort on Java

What is an efficient way to implement the Mergesort algorithm in Java such that it meets the following criteria: Must be multi-threaded. Must retain the Mergesort time complexities. Must be in-place ...
1
vote
1answer
87 views

Merge Sort Cleanup

I am re-learning my fundamental algorithms, and have been told my Python is overly verbose. Can you take a look at my merge sort algorithm and let me know how you would clean it up and make it more ...
1
vote
2answers
72 views

Mergesort, instrumented for counting reads and writes

I have just finished an implementation of mergesort, and I was wondering if I can further reduce the amount of reads and writes. I have looked at it for a while, and to my eyes, I cannot see anything ...
7
votes
2answers
1k views

MergeSort in Rust

I've just started learning Rust and put together this working MergeSort implementation. Wondering what I've overlooked and what I could improve. I couldn't figure out a cleaner way to deal with the <...
0
votes
1answer
500 views

Mergesort implementation, eliminating slice()

I am having trouble coming up an alternate solution to my mergesort. I think using slice is an expensive operation. I have two implementations, recursive and iterative. ...
4
votes
1answer
450 views

Merge Sort efficiency in Python

I am writing a script that takes in many sorted .dat files. I have included sample data but the dataset is quite large. The desired outcome is to have one file with an alphabetically sorted list of ...
2
votes
1answer
175 views

C++11 merge sort implementation

I've tried implementing merge sort in C++ just by looking at this Wikipedia graphic. Comparing my code to other merge sort code implementations, it doesn't look quite the same at first glance. Have I ...
2
votes
2answers
809 views

Merge sort a given list in Python

Problem: Implement merge sort in python. Don’t use Python’s built in sort or sorted. Assume your inputs will be sufficient for the memory you have. How should I make this code better? ...
2
votes
2answers
424 views

Generic Merge Sort in C++

I wanted to implement merge sort in a generic way. I was not sure whether I should use the iterator or the container for the template type. I did not find a way to infer the container type using ...
2
votes
1answer
41 views

C - non-recursive mergesort on lists

Continuing my foray into classic structures and algorithms, I've tried to implement merge sort for doubly-linked lists. From my (limited) testing it seems the thing I've concocted works, but several ...
4
votes
2answers
509 views

Bottom-up singly linked list merge sort in C++

This is an implementation of a singly linked list merge sort I did on paper during a job interview recently, using a bottom-up approach. The requirement was simply "implement a merge sort for a ...
4
votes
1answer
160 views

Recursive Ruby merge sort

I have implemented a merge sort in Ruby. (Yes, I know, there's Array#sort) I would like to get feedback on code quality and optimization. ...
2
votes
1answer
370 views

MergeSort implementation in Python

I wanted to share a bit about how to implement your merge sort. Merge sort is a divide and conquer algorithm that breaks down an array into smaller pieces and eventually pieces them back together one ...