in-place

In-Place Radix Sort

这一生的挚爱 提交于 2019-12-17 10:07:12
问题 This is a long text. Please bear with me. Boiled down, the question is: Is there a workable in-place radix sort algorithm ? Preliminary I've got a huge number of small fixed-length strings that only use the letters “A”, “C”, “G” and “T” (yes, you've guessed it: DNA) that I want to sort. At the moment, I use std::sort which uses introsort in all common implementations of the STL. This works quite well. However, I'm convinced that radix sort fits my problem set perfectly and should work much

Sort a part of a list in place

≯℡__Kan透↙ 提交于 2019-12-17 09:18:06
问题 Let's say we have a list: a = [4, 8, 1, 7, 3, 0, 5, 2, 6, 9] Now, a.sort() will sort the list in place. What if we want to sort only a part of the list, still in place? In C++ we could write: int array = { 4, 8, 1, 7, 3, 0, 5, 2, 6, 9 }; int * ptr = array; std::sort( ptr + 1, ptr + 4 ); Is there a similar way in Python? 回答1: 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

Move all odd positioned element to left half and even positioned to right half in-place

為{幸葍}努か 提交于 2019-12-17 08:54:08
问题 Given an array with positive and negative integers, move all the odd indexed elements to the left and even indexed elements to the right. The difficult part of the problem is to do it in-place while maintaining the order. e.g. 7, 5, 6, 3, 8, 4, 2, 1 The output should be: 5, 3, 4, 1, 7, 6, 8, 2 If the order didn't matter, we could have been used partition() algorithm of quick sort. How to do it in O( N )? 回答1: Get largest sub-array having size 3 k +1 Apply cycle leader algorithm to the parts

What in place sort when two arrays are in order?

好久不见. 提交于 2019-12-12 09:56:03
问题 I am working on this question. My function prototype is static void Sort(byte[] arr, int leftPos, int rightPos) In the 2nd part of the function i know leftPos to leftPos + (rightPos-leftPos)/2 and (rightPos-leftPos)/2 to rightPos are sorted in order. I tried thinking of how i could do an in place sort knowing the two parts are in order. I couldnt think of any. I looked at the merge function on merge sort but it uses an output array rather than in place. How do i sort it in place knowing both

javascript sort of HTML elements in-place [closed]

半城伤御伤魂 提交于 2019-12-11 00:07:53
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . in continuation for javascript sort of HTML elements I want to sort elements in different <ol> ( eventually in <div> ) the sort should preferably be in-place and not to unwrap() , sort() and wrap HTML <ol class=

Altering numpy function output array in place

不羁的心 提交于 2019-12-10 13:39:11
问题 I'm trying to write a function that performs a mathematical operation on an array and returns the result. A simplified example could be: def original_func(A): return A[1:] + A[:-1] For speed-up and to avoid allocating a new output array for each function call, I would like to have the output array as an argument, and alter it in place: def inplace_func(A, out): out[:] = A[1:] + A[:-1] However, when calling these two functions in the following manner, A = numpy.random.rand(1000,1000) out =

CUDA In-place Transpose Error

懵懂的女人 提交于 2019-12-10 05:58:14
问题 I'm implementing a CUDA program for transposing an image. I created 2 kernels. The first kernel does out of place transposition and works perfectly for any image size. Then I created a kernel for in-place transposition of square images. However, the output is incorrect. The lower triangle of the image is transposed but the upper triangle remains the same. The resulting image has a stairs like pattern in the diagonal and the size of each step of the stairs is equal to the 2D block size which I

How to remove duplicates from a file and write to the same file?

余生长醉 提交于 2019-12-09 15:14:23
问题 I know my title is not much self-explanatory but let me try to explain it here. I have a file name test.txt which has some duplicate lines. Now, what I want to do is remove those duplicate lines and at the same time update test.txt with the new content. test.txt AAAA BBBB AAAA CCCC I know I can use sort -u test.txt to remove the duplicates but to update the file with new content how do I redirect it's output to the same file. The below command doesn't work. sort -u test.txt > test.txt So, why

Which operator (+ vs +=) should be used for performance? (In-place Vs not-in-place)

為{幸葍}努か 提交于 2019-12-08 15:24:30
问题 Let's say I have this two snippet of code in python : 1 -------------------------- import numpy as np x = np.array([1,2,3,4]) y = x x = x + np.array([1,1,1,1]) print y 2 -------------------------- import numpy as np x = np.array([1,2,3,4]) y = x x += np.array([1,1,1,1]) print y I thought the result of y will be the same in both examples since y point out to x and x become (2,3,4,5) , BUT it wasn't The results were (1,2,3,4) for 1 and (2,3,4,5) for 2 . After some research I find out that in

Have the `__rdiv__()` and `__idiv__` operators changed in Python 3.x?

浪尽此生 提交于 2019-12-07 00:22:42
问题 In Python 3.x the __div__() operator was deprecated in favor of __floordiv__() and __truediv__() . Are the __rdiv__() and __idiv__() operators still used to refer to the reversed and in-place versions of __truediv__() ? If so, what are the operator names for the reversed and in-place versions of __floordiv__() ? 回答1: __rdiv__ and __idiv__ no longer exist. The new names are the obvious choices of __rtruediv__ , __itruediv__ , __rfloordiv__ , and __ifloordiv__ , following the standard format.