Is Quicksort in-place or not? [duplicate]

Intro to Algorithms from MIT Press qualifies QuickSort as in-place – it sorts the elements within the array with at most a constant amount of them outside the array at any given time. At the end of the day, people will always have differing opinions (is Top-Down Memoization considered Dynamic Programming? Not to some “classical” … Read more

Pandas: peculiar performance drop for inplace rename after dropna

This is a copy of the explanation on github. There is no guarantee that an inplace operation is actually faster. Often they are actually the same operation that works on a copy, but the top-level reference is reassigned. The reason for the difference in performance in this case is as follows. The (df1-df2).dropna() call creates … Read more

Sort a part of a list in place

I’d write it this way: a[i:j] = sorted(a[i:j]) It is not in-place sort either, but fast enough for relatively small segments. Please note, that Python copies only object references, so the speed penalty won’t be that huge compared to a real in-place sort as one would expect.

How to remove trailing whitespaces for multiple files?

You want sed –in-place ‘s/[[:space:]]\+$//’ file That will delete all POSIX standard defined whitespace characters, including vertical tab and form feed. Also, it will only do a replacement if the trailing whitespace actually exists, unlike the other answers that use the zero or more matcher (*). –in-place is simply the long form of -i. I … Read more

Updating a java map entry

Use table.put(key, val); to add a new key/value pair or overwrite an existing key’s value. From the Javadocs: V put(K key, V value): Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. … Read more

Python Math – TypeError: ‘NoneType’ object is not subscriptable

lista = list.sort(lista) This should be lista.sort() The .sort() method is in-place, and returns None. If you want something not in-place, which returns a value, you could use sorted_list = sorted(lista) Aside #1: please don’t call your lists list. That clobbers the builtin list type. Aside #2: I’m not sure what this line is meant … Read more

Difference between a -= b and a = a – b in Python

Note: using in-place operations on NumPy arrays that share memory in no longer a problem in version 1.13.0 onward (see details here). The two operation will produce the same result. This answer only applies to earlier versions of NumPy. Mutating arrays while they’re being used in computations can lead to unexpected results! In the example … Read more