programming-pearls

Sorting 10 million integers with 1 MB space Solution explanation - Programming Pearls

我只是一个虾纸丫 提交于 2019-12-21 21:16:17
问题 I was reading "Programming Pearls" and I am really confused in one of the solution explanations. The question was: "A file containing at most n positive integers, each less than n, where n = 10^7. Each positive integer could appear at most ten times. How would you sort the file?" Given solution in the book: " If each integer appears at most ten times, then we can count its occurence in a four-bit half byte. Using the solution to Problem 5 (below) we can sort the complete file in a single pass

Fastest algorithm for circle shift N sized array for M position

╄→尐↘猪︶ㄣ 提交于 2019-12-17 06:30:12
问题 What is the fastest algorithm for circle shifting array for M positions? For example, [3 4 5 2 3 1 4] shift M = 2 positions should be [1 4 3 4 5 2 3] . Thanks a lot. 回答1: If you want O(n) time and no extra memory usage (since array was specified), use the algorithm from Jon Bentley's book, "Programming Pearls 2nd Edition". It swaps all the elements twice. Not as fast as using linked lists but uses less memory and is conceptually simple. shiftArray( theArray, M ): size = len( theArray ) assert

Fastest algorithm for circle shift N sized array for M position

﹥>﹥吖頭↗ 提交于 2019-12-17 06:30:10
问题 What is the fastest algorithm for circle shifting array for M positions? For example, [3 4 5 2 3 1 4] shift M = 2 positions should be [1 4 3 4 5 2 3] . Thanks a lot. 回答1: If you want O(n) time and no extra memory usage (since array was specified), use the algorithm from Jon Bentley's book, "Programming Pearls 2nd Edition". It swaps all the elements twice. Not as fast as using linked lists but uses less memory and is conceptually simple. shiftArray( theArray, M ): size = len( theArray ) assert

bsort example from programming pearls

筅森魡賤 提交于 2019-12-11 05:19:45
问题 In Programming Pearls there is an algorithm that sorts varying length arrays but sorts in time proportional to the sum of their length. For example, if we have a record array x[0...n-1] , and each record has an integer length and a pointer to array bit[0...length-1] . The code is implemented this way: void bsort(l, u, depth){ if (l >= u) return ; for (i = l; i <= u; i++){ if (x[i].length < depth) swap(i, l++); } m = l; for (int i = l; i < u; i++){ if (x[i].bit[depth] == 0) swap(i, m++); }

Longest Non-Overlapping Substring

折月煮酒 提交于 2019-12-06 03:38:38
问题 I wonder if anyone knows the (optimal?) algorithm for longest recurring non-overlapping sub string. For example, in the string ABADZEDGBADEZ the longest recurring would be "BAD". Incidentally if there is no such result, the algorithm should alert that such a thing has occurred. My guess is that this involves suffix trees. I'm sure this must exist somewhere already. Thanks for the help! 回答1: Since you're applying this to music, you're probably not looking for 100% matches; there will be

Longest Non-Overlapping Substring

自闭症网瘾萝莉.ら 提交于 2019-12-04 08:45:43
I wonder if anyone knows the (optimal?) algorithm for longest recurring non-overlapping sub string. For example, in the string ABADZEDGBADEZ the longest recurring would be "BAD". Incidentally if there is no such result, the algorithm should alert that such a thing has occurred. My guess is that this involves suffix trees. I'm sure this must exist somewhere already. Thanks for the help! Since you're applying this to music, you're probably not looking for 100% matches; there will be expected discrepencies from one instance of the theme to another. You might try looking up gene sequence analysis

How do the bit manipulations in this bit-sorting code work?

风流意气都作罢 提交于 2019-12-04 07:57:06
问题 Jon Bentley in Column 1 of his book programming pearls introduces a technique for sorting a sequence of non-zero positive integers using bit vectors. I have taken the program bitsort.c from here and pasted it below: /* Copyright (C) 1999 Lucent Technologies */ /* From 'Programming Pearls' by Jon Bentley */ /* bitsort.c -- bitmap sort from Column 1 * Sort distinct integers in the range [0..N-1] */ #include <stdio.h> #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000

How do the bit manipulations in this bit-sorting code work?

回眸只為那壹抹淺笑 提交于 2019-12-02 18:51:21
Jon Bentley in Column 1 of his book programming pearls introduces a technique for sorting a sequence of non-zero positive integers using bit vectors. I have taken the program bitsort.c from here and pasted it below: /* Copyright (C) 1999 Lucent Technologies */ /* From 'Programming Pearls' by Jon Bentley */ /* bitsort.c -- bitmap sort from Column 1 * Sort distinct integers in the range [0..N-1] */ #include <stdio.h> #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000 int a[1 + N/BITSPERWORD]; void set(int i) { int sh = i>>SHIFT; a[i>>SHIFT] |= (1<<(i & MASK)); } void clr

Bit Mask usage in the program below from Programming Pearls

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 04:08:17
I started reading "Programming Pearls" today and while doing it's exercise I came across this question "How would you implement your own bit vector?". When I looked at the solution it was like this: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000 int a[1 + N/BITSPERWORD]; void set(int i) { a[i >> SHIFT] |= (1 << (i & MASK)); Where I am getting confused at is this statement 1 << (i & MASK) Could someone please explain me what's going on here? Note that MASK is set such that it has the lowest SHIFT bits set, where SHIFT is exactly the base-2 logarithm of BITSPERWORD .

Bit Mask usage in the program below from Programming Pearls

核能气质少年 提交于 2019-12-01 01:33:57
问题 I started reading "Programming Pearls" today and while doing it's exercise I came across this question "How would you implement your own bit vector?". When I looked at the solution it was like this: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000 int a[1 + N/BITSPERWORD]; void set(int i) { a[i >> SHIFT] |= (1 << (i & MASK)); Where I am getting confused at is this statement 1 << (i & MASK) Could someone please explain me what's going on here? 回答1: Note that MASK is