Skip to main content
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.7k
51 votes
Accepted

"How are you spending your time on the computer?"

Performance [..] winforms is not powerful so it takes 1-2 seconds to refresh the content [..] It's not WinForms because it's actually very fast and I have never had any issues with it. There is a ...
t3chb0t's user avatar
  • 44.3k
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
41 votes
Accepted

Given an array of integers, return the smallest positive integer not in it

That's a nice simple solution, with two problems: It will give incorrect result when A contains all the values in the ranges ...
janos's user avatar
  • 111k
33 votes

Given an array of integers, return the smallest positive integer not in it

How about this solution, which – if I'm not mistaken – should fulfill all requirements: create a second array run through all elements of the input array for each number set the respective key in the ...
insertusernamehere's user avatar
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
30 votes

Given an array of integers, return the smallest positive integer not in it

In order to satisfy the O(N) time-complexity, construct a Set() in O(N) time and space complexity, then use a while loop which ...
Patrick Roberts's user avatar
20 votes
Accepted

Pancake sort interview with Python

flip For starter, you can swap two variables in Python without using a temporary one: ...
301_Moved_Permanently's user avatar
18 votes

Milking order compatible with maximum number of observations

1. Review Python's style guide recommends 4 spaces for each level of indentation, and keeping lines to a maximum of 79 characters. It's not compulsory to follow this style, but it makes it easier for ...
Gareth Rees's user avatar
  • 49.7k
16 votes
Accepted

A Lisp version of the Schwartzian transform

What is not enough LISP-y in your function is the style used for the parentheses (they should not be left alone as last element of a line and they should be on the last line of the enclosed form). ...
Renzo's user avatar
  • 2,055
16 votes
Accepted

Sorting dictionary alphabetically

I'm afraid your sorting is in vain because the normal dictionary does not guarantee that the items will be enumerated in the same order as you added them: For purposes of enumeration, each item in ...
t3chb0t's user avatar
  • 44.3k
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

"How are you spending your time on the computer?"

First thing I would do is change if (mainString.Length == desiredLength) to if (mainString.Length >= desiredLength), since if ...
Der Kommissar's user avatar
15 votes
Accepted

Merge n sorted iterators

There are no docstrings. What do these functions do? What arguments do they take? What do they return? Decorators are normally named after the effect they have on the decorated function. For example <...
Gareth Rees's user avatar
  • 49.7k
15 votes
Accepted

Sort int array by frequency then value

Headers ...
Loki Astari's user avatar
  • 95.7k
14 votes
Accepted

Minimum number of swaps required to sort the array in ascending order

Look for clues My time complexity is \$O(n^2)\$. Can I reduce it? When the program appears to work correctly for all simple examples you tried, but the time limit is exceeded when you submit, that's ...
janos's user avatar
  • 111k
14 votes
Accepted

Sorting algorithm visualizer in C++ and SDL2

In all, this is a nice program. In particular, it compiled and ran (almost) flawlessly on Linux, so keep up the good work on portability! Here are some things that may help you improve your program. ...
Edward's user avatar
  • 66.5k
14 votes

Sorting array by odd numbers while leaving the even numbers in the same spots

Naming: avoid misleadingly generic names. If a function does something unusual (like sorting only the odd values in a sequence) don't be satisfied with a name implying that it does something ordinary (...
FMc's user avatar
  • 12.8k
13 votes

Bubblesort implementation in C++

Performance of your implementation is \$\Theta(N^2)\$ in all cases. You can improve it by holding a boolean variable indicating whether the current pass over the array swapped at least once the ...
coderodde's user avatar
  • 29.1k
13 votes
Accepted

Sorting algorithm, possibly a new one

Algorithm What you have implemented is called (linear) insertion sort. It is very old, sorry your method is not new or novel ;) It has average case O(n^2) time complexity which is basically unusable ...
Emily L.'s user avatar
  • 16.6k
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.7k
13 votes
Accepted

Algorithm "sort except zero"

Most of the work will be done by your call to sorted. You just need to make sure to not make too much extra fluff around that call. One particular call that may ...
301_Moved_Permanently's user avatar
12 votes
Accepted

Building unusual IComparer<T> from expressions

...
Peter Taylor's user avatar
  • 24.1k
12 votes

My approach to sorting algorithm

Since the goal is the best possible implementation of this algorithm, I'd suggest the following. However, faster algorithms do exist. To conform to PEP8 make sure you have two blank lines after your ...
Luapulu's user avatar
  • 333
12 votes
Accepted

Count word frequencies, and print them most-frequent first

Mainly this looks fine. using namespace std; is a bad habit to get into due to potential name collisions. Instead prefer to type ...
user673679's user avatar
  • 11.8k
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
  • 78.9k
11 votes
Accepted

Sorting algorithms - Bubble sort

I recommend putting your functions into a namespace. for (auto i = begin + 1; i != end; ++i) { By doing begin + 1, you ...
Justin's user avatar
  • 3,295
11 votes

Sorting algorithms - Bubble sort

std::swap(val1, val0); This hard codes std::swap as the function to use for swapping. C++ also has a powerful (and sometimes ...
Daniel Jour's user avatar
  • 1,581
11 votes

Sorting algorithms - Bubble sort

Speed Of course we all know bubble sort is one of the slowest sorting algorithms1. It turns out, however, that stopping when an iteration is completed without a swap usually adds more overhead than ...
Jerry Coffin's user avatar
  • 33.6k
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

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