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 “...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
20
votes
Accepted
Pancake sort interview with Python
flip
For starter, you can swap two variables in Python without using a temporary one:
...
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 ...
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). ...
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 ...
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 ...
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 ...
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 <...
15
votes
Accepted
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 ...
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.
...
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 (...
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 ...
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 ...
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.
...
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 ...
12
votes
Accepted
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sorting × 1160algorithm × 323
java × 269
python × 211
c++ × 195
performance × 149
mergesort × 142
javascript × 126
beginner × 111
c × 101
array × 100
c# × 84
quick-sort × 79
python-3.x × 65
insertion-sort × 46
programming-challenge × 45
c++11 × 33
reinventing-the-wheel × 33
strings × 29
recursion × 27
radix-sort × 27
time-limit-exceeded × 26
php × 24
hash-map × 23
ruby × 22