In-place radix sort in D programming language
我正在尝试从就地基数排序工作中获取就地基数排序示例。到目前为止,我有:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import std.random; void swap(ref string i,ref string j) { string tmp = i; i = j; j = tmp; } void radixSort(ref string[] seqs, size_t base = 0) { if(seqs.length == 0) return; size_t TPos = seqs.length, APos = 0; size_t i = 0; while(i < TPos) { if(seqs[i][base] == 'A') { swap(seqs[i], seqs[APos++]); i++; } else if(seqs[i][base] == 'T') { swap(seqs[i], seqs[--TPos]); } else i++; } i = APos; size_t CPos = APos; while(i < TPos) { if(seqs[i][base] == 'C') { swap(seqs[i], seqs[CPos++]); } i++; } if(base < seqs[0].length - 1) { radixSort(seqs[0..APos], base + 1); radixSort(seqs[APos..CPos], base + 1); radixSort(seqs[CPos..TPos], base + 1); radixSort(seqs[TPos..seqs.length], base + 1); } } void main(string[] args) { string [] sequences; for(int n=0;n<10;n++) { string seq; for(int i=0;i<10;i++) { int r = rand()%4; if(r == 0) seq = seq ~"A"; if(r == 1) seq = seq ~"C"; if(r == 2) seq = seq ~"G"; if(r == 3) seq = seq ~"T"; } sequences = sequences ~ seq; } writefln("Unsorted"); for(size_t n=0;n<10;n++) { writefln(sequences[n]); } radixSort(sequences,0); writefln("Sorted"); for(size_t n=0;n<10;n++) { writefln(sequences[n]); } } |
但是,失败的原因是:
1 2 3 4 | radix.d(36): Error: slice expression seqs[0u..APos] is not a modifiable lvalue radix.d(37): Error: slice expression seqs[APos..CPos] is not a modifiable lvalue radix.d(38): Error: slice expression seqs[CPos..TPos] is not a modifiable lvalue radix.d(39): Error: slice expression seqs[TPos..seqs.length] is not a modifiable lvalue |
号
在数字火星D编译器v1.066下。我想切片不是可变的,但是…我该怎么处理这个?
我是D的新手,主要是想让这个例子工作起来。
如果要修改引用本身,只需要