12 votes
Accepted

Insertion sort in Rust

As you have already been made aware, you should not be using &mut Vec<T> unless you plan on adding or removing items from the ...
Shepmaster's user avatar
  • 8,628
11 votes
Accepted

Benchmarking insertion sort

Your initial claim sounds about right to me, since for each iteration, checking at most cutoff elements for the insertion_point ...
Emma X's user avatar
  • 423
8 votes
Accepted

Insertion sort with high OOP and error processing

Let's start by considering how this might look if you were just using std::sort instead of writing a sort algorithm of your own. Such code might look something ...
Jerry Coffin's user avatar
  • 33.5k
7 votes
Accepted

Time limit exceeded for java "insertion sort linked list"

The culprit is i = jn; in the last else branch. This is not needed because you know that the entire list from head to ...
ratchet freak's user avatar
7 votes

Insertion sort with high OOP and error processing

I'll shoot for your main incentive to write this code - the lower level C++ details have been attacked by Jerry already. The main task was to implement OOP. I see not a single hint of OOP (object ...
AnoE's user avatar
  • 171
6 votes
Accepted

Insertion sort C++14 implementation

Advice 1 In your insertionSort you can remove the last statement (return). Advice 2 Making your insertion sort generic is ...
coderodde's user avatar
  • 27.6k
5 votes

Sorting algorithms - Insertion sort

Important stuff: Missing header: The std::less<> is from <functional>. Relying on type deduction: Although it is ...
Incomputable's user avatar
  • 9,444
5 votes
Accepted

Insertion Sort Implementation Using For And While Loop

Thoughts on best practices Method signature should be void and should simply return the modified original array. Again, is this behavior better / more-expected? My argument against this is that ...
mdfst13's user avatar
  • 21.5k
5 votes

Insertion sort via recursion

A simple in-place, iterative version: import bisect def insertsort(l): for nsorted in range(1, len(l)): bisect.insort(l, l.pop(), hi=nsorted) then, ...
della's user avatar
  • 151
5 votes

Selection and Insertion sorts from scratch in Java

Please try to follow the code formatting conventions of the language that you're using. This makes your code much more readable for other programmers. If you use a Java IDE like Eclipse, NetBeans or ...
fap's user avatar
  • 159
4 votes

Insertion sort implementation in C++

In both this and your bubble sort implementation you need to start using C++ idioms (otherwise this is just C code (that's fine C is a great language it's just not C++)). I would say this is the ...
Martin York's user avatar
  • 94.6k
4 votes
Accepted

Insertion sort implementation in Python

TL;DR Be as specific as you can: When naming functions, And when setting up loops. Details: Here's what I've noticed in your code, line-by-line: def run(input) ...
Ryan Mills's user avatar
  • 1,010
4 votes
Accepted

Optimizing insertion sort for small amounts of data

Unfortunately, you are unlikely to find a better sort than the linear insertion sort when it comes to sorting small data sets. The only sorting algorithms that might be faster are sorting algorithms ...
Morwenn's user avatar
  • 19.9k
3 votes

Sorting algorithms - Insertion sort

if (std::distance(begin, end) < 2) { return; } While distance is constant complexity for random access ...
Snowhawk's user avatar
  • 6,138
3 votes

Bisect Insertion Container for Calculating Running Median

This var values = new[] { bound.ToString() }.Concat(..).ToArray(); RunBiSect(values); plus this ...
t3chb0t's user avatar
  • 44.2k
3 votes

Java insertion sort implementation

Code First of all, please avoid having a static int temp: in case two or more threads call your insertion sort, they will interfere. Also, I would declare the ...
coderodde's user avatar
  • 27.6k
3 votes

Insertion Sort in Python

Your solution looks ok to me. But let's understand better how this sort method actually works. The insertion sort, works as it follows: always maintains a sorted sublist in the lower positions of the ...
Grajdeanu Alex's user avatar
3 votes

Insertion Sort in C++

Extending the answer from @Clearer, which doesn't use the templated custom comparison class. Here is how I modified it to use it: ...
dfreese's user avatar
  • 131
3 votes

Sorting an array of strings using pointers (followup)

It's not necessary to cast the result of malloc(). A void* can be assigned to any other kind of pointer in C, so write ...
Toby Speight's user avatar
  • 74.4k
3 votes
Accepted

C++ insertion sort implementation

Small nitpicks The for loop can be started from 1, as the iteration with 0 doesn't actually do anything. Prefer to reduce the scope of variables if possible. This allows better reasoning for the ...
hoffmale's user avatar
  • 6,468
3 votes
Accepted

Insertion Sort and Selection Sort Implementation

First tip: use more aggressive warnings, this way you would be warned by the compiler about passing unused parameters into functions, that is you would automatically made: ...
paler123's user avatar
  • 291
3 votes

Python: Insertion Sort

There is a much simpler method in Python to do an insertion sort using the bisect.insort method ...
MrJoe's user avatar
  • 2,033
3 votes
Accepted

Comparing binary insertion sort with straight insertion sort in Java

The only reason BinaryInsertionSort outperforms StraightInsertionSort is that it is in the position to call ...
vnp's user avatar
  • 56.3k
3 votes

Hybrid Merge/Insertion sort algorithm

Optimization notes. Allocate the temporary array once, and pass it down to the recursive invocations. Do not fall back to the insertion sort immediately. Postpone it until all the recursions ...
vnp's user avatar
  • 56.3k
3 votes
Accepted

Sorting and Searching Algorithm

The code is overcommented. Most of the comments do not add any value. They explain what the code is doing, which is obvious; the good comment shall explain why and how the code is doing what it is ...
vnp's user avatar
  • 56.3k
3 votes
Accepted

Insertion Sort Implemented in Ruby

Consistency Sometimes you are using 4 spaces for indentation, sometimes 2. Sometimes you are using vertical whitespace (an empty line) before a method definition, sometimes you don't. Sometimes you ...
Jörg W Mittag's user avatar
2 votes

Insertion sort in JavaScript

Eliminating some unnecessary code Some programmers find break statements to be problematic. If you'd like to know more, here is a programmers stack exchange ...
Joshua Dawson's user avatar
2 votes

Hackerrank Insertion Sort Part 2

No naked loops The inner loop implements an insert algorithm, and deserves a name. Factor it out into a method. It may or may not incur a performance penalty; I ...
vnp's user avatar
  • 56.3k
2 votes
Accepted

Hackerrank Insertion Sort Part 2

Your code is pretty good. The biggest problem is consistent indentation. Everything inside of your class should be indented one level; this includes the main method and your ...
Justin's user avatar
  • 3,295
2 votes

Hackerrank Insertion Sort Part 2

Your implementation is quite fine. Some minor improvements are possible. The insertionsort method doesn't need to return the array. It's not required by the ...
janos's user avatar
  • 111k

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