ptr++
mid, last := &ptr[d], &ptr[n]
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
/**
Please references
http://www.cplusplus.com/reference/algorithm/rotate/
Time complexity: O(n)
*/
func rotateIndexAdv(s []int, d int) {
n := len(s)
//mid points to the beginning of the right side of swapping, first, next are moving pointer(index) to where the next targets of both sides
first, next, mid := 0, d, d
for first != next {
s[first], s[next] = s[next], s[first]
first++; next++
if next == n {
next = mid //back to the beginning of mide
} else if first == mid {
mid = next //moving mid point to another set
}
}
}
The one I came up with before my first post is the following:
/**
Please Reference.
http://www.geeksforgeeks.org/array-rotation/
http://stackoverflow.com/questions/21160875/why-is-stdrotate-so-fast, the second post
Time complexity: O(n)
Auxillary space O(1)
*/
func rotateArrayAdv(s []int, d int) {
n := len(s)
var j, k int
for i := 0; i < gcd(n, d); i++ {
temp := s[i]
j = i
for {
k = j + d
//It treats thw whole series as circular, a,b,c,d,e,f,g (a,b,c)
if k >= n {
k -= n
}
if k == i {
break
}
s[j] = s[k]
j = k
}
s[j] = temp
}
}The first one is nice & compact but not easy to understand. simulating using an example like rotate({1,2,3,4,5,6,7}, 3) would help. The second one is an amazing one. If the gcd = 1, the program would touch every index of the slice but never repeat.(It will finish a cycle) That is based upon modular arithmetic.
I need some clarification. The book has already given the rotate method using three reverse. The exercise require us to come up with one-pass solution. Therefore, it is alternative thinking exercise. There is also another simple and understandable method. I call it rotateCopyRestore which use temporary storage. I don't consider it as one-pass solution either.
I agrees with Matt's correction on swap time complexity analysis. However, I presented two one-pass methods. I don't know which one people consider as cyclic permutations. However, rotateArrayAdv does not involve any swap, It just keep moving value from d * (i+1) % n to d * i % n.
I am learning the impact of algorithm methods on performance. Michael, can you explain further regarding to "For these types of array indexing computations, the redundant bounds checks are expensive."
I guess you are right. I was talking about the algorithm taken from C++ doing n swaps. I didn't take the time to analyze the one with the gcd.
On Thursday, March 31, 2016 at 5:13:37 AM UTC+2, Matt Harden wrote:
So I'm not arguing which is faster; I don't doubt that reversing is more performant on modern CPUs. I also agree that a "cleverer" (read: more complex in terms of human understanding) algorithm that's demonstrably slower and doesn't reduce some other resource use like memory or locking is a worse algorithm. Nevertheless, I must take issue with one thing Roberto said. The "one pass" algorithm that uses cyclic
BenchmarkRotateEZ10-8 10000000 188 ns/op
BenchmarkRotateEZ100-8 10000 101978 ns/opBenchmarkRotateEZ1000-8 30 48516706 ns/op{skipped BenchmarkRotateEZ1000}BenchmarkRotateFM10-8 10000000 152 ns/opBenchmarkRotateFM100-8 200000 10355 ns/opBenchmarkRotateFM1000-8 2000 862210 ns/opBenchmarkRotateFM10000-8 20 79812929 ns/opBenchmarkRotateMH10-8 10000000 151 ns/opBenchmarkRotateMH100-8 200000 10083 ns/opBenchmarkRotateMH1000-8 2000 805174 ns/op
BenchmarkRotateMH10000-8 20 77660177 ns/oppermutations does not perform n swaps. If you consider a swap to be 2 reads followed by 2 writes, as in reversing an array, then the one pass algorithm also requires n/2 swaps, just like reversing an array just one time. It's exactly one read and one write of each element of the array, which is obviously the minimum possible.
On Wed, Mar 30, 2016 at 7:08 PM Michael Jones <m...@wearality.com> wrote:Credit to Ken Thompson. Apparently that’s how he did it in ‘ed'
—Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022On Mar 30, 2016, at 6:20 PM, Roberto Zanotto <roby...@gmail.com> wrote:The "one pass" thing is misleading:the one pass algorithm does n swaps, the two passes algorithm still does n swaps, as reversing an array takes n/2 swaps.Also, reversing is probably more cache friendly.The algorithm based on reverse is just plain better.
On Thursday, March 31, 2016 at 2:38:26 AM UTC+2, Michael Jones wrote:I understand now that this was a “how to code cleverly” question. One pass is quite clever.However, two passes (MH) is simpler and requires half the time.I hope they don’t teach slow tricks in school!Compiled Normally
...
...