Skip to main content
The 2024 Developer Survey results are live! See the results
55 votes

Sorting large 1GB file with 100 millions numbers using merge sort

Don't do this using namespace std; This is a bad habit that will get you into a lot of trouble. Please break the habit before it becomes ingrained. see: Why is “...
Loki Astari's user avatar
  • 95.5k
45 votes

Sorting large 1GB file with 100 millions numbers using merge sort

Avoid std::endl At least at first glance, it looks like you've committed the single largest sin in C++ with respect to file I/O speed: you've used ...
Jerry Coffin's user avatar
  • 33.6k
30 votes

Sorting large 1GB file with 100 millions numbers using merge sort

Don't use using namespace std; in global scope, that's a bad habit to get into. If you really dislike writing std:: over and ...
ratchet freak's user avatar
16 votes

Python3 - merge sort, O(n) space efficiency

Merge is usually O(m) time, where m is the number of elements involved in the merge. Due to your insertions and deletions, it's rather O(mn), where n is the length of the entire list. That makes your ...
superb rain's user avatar
  • 3,991
15 votes

Merge two already sorted linked list

Here are some things that may help you improve your code. Don't abuse using namespace std Putting using namespace std at the ...
Edward's user avatar
  • 66.5k
14 votes
Accepted

C++ merge sort implementation (looking for advice)

Avoid unnecessary copies Your algorithm passes vectors solely by value. That means a copy is made each time. This can be very expensive for large arrays. Consider passing vectors that are not going to ...
G. Sliepen's user avatar
  • 58.9k
13 votes
Accepted

Merge sort implementation with vectors

Overview Good first try. It seems like it works. But currently it only works for vectors and ints. Some thoughts for improving the algorithm in the future. If you look at most C++ algorithms they ...
Loki Astari's user avatar
  • 95.5k
13 votes
Accepted

C++ Merge Sort Implementation

Your big mistake is not using iterators. As a result you pass your vectors by value, which causes the vectors to be copied a lot. ...
Loki Astari's user avatar
  • 95.5k
12 votes
Accepted

Recursive merge sort in python

First of all, there is a bug in the code - if an array is empty, you'll get a "maximum recursion depth exceeded" error. Improve your base case handling: ...
alecxe's user avatar
  • 17.3k
12 votes

C++ code to sort a list of elements

#include<iostream> #include<vector> #include<random> #include<chrono> Leave some space between the ...
papagaga's user avatar
  • 5,757
11 votes

Sorting large 1GB file with 100 millions numbers using merge sort

Use your knowledge of the domain You may be able to compare numbers without parsing them, by working with strings instead. First compare the sign characters (if present), then skip leading zeros, ...
Toby Speight's user avatar
  • 77.3k
11 votes

C++ Merge Sort Implementation

Yes, it's a mergesort, switching to something else for small ranges is a common refinement. Be aware your implementation is not stable, though it doesn't matter for ...
Deduplicator's user avatar
  • 19.3k
11 votes
Accepted

Mergesort Criticism in C

Avoid code duplication Your code is more verbose than necessary, because you are repeating a lot of things unnecessarily, or write things in a more complex way than necessary. For example: ...
G. Sliepen's user avatar
  • 58.9k
10 votes

Merging two files containing sorted numbers into one

As the others have pointed out, there is a lot of repetition in your code. This is however a great opportunity to threat the files as simple iterators from which you can extract integers. This is ...
D. Jurcau's user avatar
  • 3,010
10 votes
Accepted

Merge Sort implemented using iterators

Naming sub_merge is weird, as it doesn't match up at all with the meaning of the english word "submerge". I get its origin ("subroutine merge"), but why not simply ...
hoffmale's user avatar
  • 6,498
10 votes

Recursive merge sort in C

Handle size 0 // if (num_elements == 1) { if (num_elements <= 1) { return; } Sorting double should handle not-a-number ...
chux - Reinstate Monica's user avatar
9 votes

Merge Sort implemented using iterators

Well, interesting. There's a bug in merge_sort(): Subtracting 1 from end, without knowing ...
Deduplicator's user avatar
  • 19.3k
8 votes

Merge two already sorted linked list

A proper C++ implementation would be generic, i.e. a code that can merge any linked lists (of the same type). This could be implemented by taking iterators (and templating over iterator type). At the ...
Walter's user avatar
  • 465
8 votes

Merge two already sorted linked list

Note: I'm at least trying to avoid repeating @Edward's points, which I think are all quite good. It looks like while I was writing this @Loki wrote a few things that are a lot closer to what I said--...
Jerry Coffin's user avatar
  • 33.6k
8 votes
Accepted

Generic merge sort in C++ using a user-defined comparison function or the < operator by default

Avoid a copy by passing by const reference. ...
Loki Astari's user avatar
  • 95.5k
8 votes
Accepted

MergeSort in Rust

Disclaimer: I'm also new to Rust, but I have a background in C, C++ and Haskell. Take everything I say with a grain of salt. All of that looks reasonable, except for the ...
Zeta's user avatar
  • 19.3k
8 votes
Accepted

Writing Merge Sort in C++

std::ceil(static_cast<double>(size) / 2 is a very long (and unclear) way to say size - size/2. No naked loops please. Each ...
vnp's user avatar
  • 56.8k
8 votes

Python3 - merge sort, O(n) space efficiency

Welcome to Code Review! PEP-8 Python has a style guide to help developers write clean, maintainable and readable code. It is referred to as PEP-8. A few points of note: Avoid extraneous whitespace in ...
hjpotter92's user avatar
  • 8,726
8 votes

Recursive merge sort in C

int main(int argc, char* argv[argc+1]) You don't need to put a size in the argv array. The compiler ignores it. You should #include <stdio.h> for the printf ...
AShelly's user avatar
  • 934
7 votes
Accepted

Merge sorting a singly-linked list in C - follow-up

Utilize const to indicate the referenced data is not changed and allow optimizations. ...
chux - Reinstate Monica's user avatar
7 votes

Merge sort C# Implementation

Inside of your copyArray you should not have the cnt variable separate from the for loop, you should just include it in the ...
Malachi's user avatar
  • 28.7k
7 votes
Accepted

Merge sort C# Implementation

Advice 1 Since I have very little exposure to C#, I will confine myself to efficiency: you can use double buffer strategy in order to eliminate copyArray, which ...
coderodde's user avatar
  • 28.9k
7 votes

Merging Two Sorted Lists in Python In Linear Time

Correctness First of all, your merging code is not correct. If you have list1 = [1, 2, 3] and list2 = [4, 5, 6], the result ...
alecxe's user avatar
  • 17.3k
7 votes
Accepted

Merge Sort C++11 C++17

Design Passing data with high and low works. But is equivalent to ...
Loki Astari's user avatar
  • 95.5k
6 votes
Accepted

Merging 3 sorted linked lists

The code works as it is, but it uses a number of local variables that aren't actually needed. An alternative approach might be this: ...
Edward's user avatar
  • 66.5k

Only top scored, non community-wiki answers of a minimum length are eligible