`
webcenterol
  • 浏览: 911066 次
文章分类
社区版块
存档分类
最新评论

一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法

 
阅读更多
<style>-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face {font-family:Cambria; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1073741899 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin-top:0cm; margin-right:0cm; margin-bottom:10.0pt; margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:12.0pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} h1 {mso-style-priority:9; mso-style-unhide:no; mso-style-qformat:yes; mso-style-link:"标题 1 Char"; mso-style-next:正文; margin-top:24.0pt; margin-right:0cm; margin-bottom:0cm; margin-left:0cm; margin-bottom:.0001pt; line-height:115%; mso-pagination:widow-orphan lines-together; page-break-after:avoid; mso-outline-level:1; font-size:16.0pt; mso-bidi-font-size:14.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; color:#365F91; mso-themecolor:accent1; mso-themeshade:191; mso-font-kerning:1.0pt;} h2 {mso-style-priority:9; mso-style-qformat:yes; mso-style-link:"标题 2 Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; line-height:173%; mso-pagination:widow-orphan lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} p.MsoHeader, li.MsoHeader, div.MsoHeader {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"页眉 Char"; margin-top:0cm; margin-right:0cm; margin-bottom:10.0pt; margin-left:0cm; text-align:center; mso-pagination:widow-orphan; tab-stops:center 207.65pt right 415.3pt; layout-grid-mode:char; border:none; mso-border-bottom-alt:solid windowtext .75pt; padding:0cm; mso-padding-alt:0cm 0cm 1.0pt 0cm; font-size:9.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} p.MsoFooter, li.MsoFooter, div.MsoFooter {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"页脚 Char"; margin-top:0cm; margin-right:0cm; margin-bottom:10.0pt; margin-left:0cm; mso-pagination:widow-orphan; tab-stops:center 207.65pt right 415.3pt; layout-grid-mode:char; font-size:9.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-noshow:yes; mso-style-priority:99; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"批注框文本 Char"; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:9.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin-top:0cm; margin-right:0cm; margin-bottom:10.0pt; margin-left:0cm; text-indent:21.0pt; mso-char-indent-count:2.0; line-height:115%; mso-pagination:widow-orphan; font-size:12.0pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} span.MsoIntenseReference {mso-style-priority:32; mso-style-unhide:no; mso-style-qformat:yes; font-variant:small-caps; color:#C0504D; mso-themecolor:accent2; letter-spacing:.25pt; font-weight:bold; text-decoration:underline; text-underline:single;} span.1Char {mso-style-name:"标题 1 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"标题 1"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:14.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; color:#365F91; mso-themecolor:accent1; mso-themeshade:191; font-weight:bold;} span.2Char {mso-style-name:"标题 2 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"标题 2"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; mso-font-kerning:0pt; mso-fareast-language:EN-US; mso-bidi-language:EN-US; font-weight:bold;} span.Char {mso-style-name:"页眉 Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:页眉; mso-ansi-font-size:9.0pt; mso-bidi-font-size:9.0pt; mso-font-kerning:0pt; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} span.Char0 {mso-style-name:"页脚 Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:页脚; mso-ansi-font-size:9.0pt; mso-bidi-font-size:9.0pt; mso-font-kerning:0pt; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} span.Char1 {mso-style-name:"批注框文本 Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:批注框文本; mso-ansi-font-size:9.0pt; mso-bidi-font-size:9.0pt; mso-font-kerning:0pt; mso-fareast-language:EN-US; mso-bidi-language:EN-US;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:879979139; mso-list-type:hybrid; mso-list-template-ids:-2200530 -26160278 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l0:level1 {mso-level-start-at:0; mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt; font-family:Wingdings; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} @list l1 {mso-list-id:1980842703; mso-list-type:hybrid; mso-list-template-ids:310389072 -1595380626 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l1:level1 {mso-level-start-at:0; mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt; font-family:Wingdings; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style> <![endif]-->

一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法

write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie

<<Data Structures and Algorithm Analysis in C++>>

--《数据结构与算法分析c++描述》 Mark Allen Weiss 人民邮电大学出版 中文版第49面, 一随机数组的三种生成算法

这一次没有截图,因为我受不了让bash运行100次的速度,怎么说来着,还是那句话,Bash根本就不适合也不是设计用来描述算法的语言,我根本就是自讨苦吃。用了最多的时间,得到的是最低的效率。。。。。。还有最扭曲的代码。但是因为工作中不用到bash,不常用用还真怕都忘了。。。现在用python的时候就常有这样的感觉,以前看的书就像白看了一样。

相对于bash来说,python的优雅一次一次的深入我心,每一次它都是代码最短的,最最优雅的,很多便捷的语法都是让人用的很痛快,虽然刚开始用的时候由于没有大括号。。。(我用c++习惯了)感觉代码就像悬在空中一样的不可靠。。。呵呵,习惯了以后,怎么看怎么爽。再加上丰富的库,绝了。

write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie

以下为实现部分:

CPP:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <algorithm>
4 #include <iterator>
5 #include <iostream>
6 usingnamespacestd;
7
8 intrandInt(inti, intj)
9 {
10 if(i > j)
11 {
12 inttemp = i;
13 i = j;
14 j = temp;
15 }
16
17 returnrand() % (j-i) + i;
18 }
19
20 voidrandArr1(intarr[], intn)
21 {
22 for(inti=0; i<n; ++i)
23 {
24 while(true)
25 {
26 // some thing don't like py
27 // because my randInt is create number
28 // in [0,n) and py is [0,n]
29 inttemp = randInt(0, n);
30 intj = 0;
31 while(j < i)
32 {
33 if(arr[j] == temp)
34 {
35 break;
36 }
37 ++j;
38 }
39 if(j == i)
40 {
41 arr[j] = temp;
42 break;
43 }
44 }
45 }
46 }
47
48 voidrandArr2(inta[], intn)
49 {
50 bool*lpb = newbool[n];
51 memset(lpb, 0, n);
52
53 for(inti=0; i<n; ++i)
54 {
55 while(true)
56 {
57 inttemp = randInt(0, n);
58 if(!lpb[temp])
59 {
60 a[i] = temp;
61 lpb[temp] = true;
62 break;
63 }
64 }
65
66 }
67
68 deletelpb;
69 lpb = NULL;
70 }
71
72 voidswap(int& a, int& b)
73 {
74 inttemp = a;
75 a = b;
76 b = temp;
77 }
78
79 // just for fun,but I like it.
80 // when py could have range and bash could have seq
81 // why cpp couldn't have a seq like this? lol
82 template<typenameT>
83 classCFBSeq
84 {
85 public:
86 CFBSeq(constT& aValue) : mValue(aValue) { }
87
88 T operator()()
89 {
90 returnmValue++;
91 }
92
93 private:
94 T mValue;
95 };
96
97 voidrandArr3(inta[], intn)
98 {
99 generate(a, a+n, CFBSeq<int>(0));
100
101 for(inti=1; i<n; ++i)
102 {
103 swap(a[i], a[randInt(0,i)]);
104 }
105
106 }
107
108
109
110
111
112 intmain(intargc, char* argv[])
113 {
114 constintTIMES=100;
115 srand(time(NULL));
116 inta[TIMES],b[TIMES],c[TIMES];
117 randArr1(a,TIMES);
118 copy(a, a+TIMES, ostream_iterator<int>(cout," "));
119 printf("/n");
120 randArr2(b,TIMES);
121 copy(b, b+TIMES, ostream_iterator<int>(cout," "));
122 printf("/n");
123 randArr3(c, TIMES);
124 copy(c, c+TIMES, ostream_iterator<int>(cout," "));
125
126
127
128 exit(0);
129 }
130



LUA:

1 #!/usr/bin/env lua
2
3 math.randomseed(os.time())
4
5 functionrandArr1(a, n)
6 fori=1,n do
7 whiletruedo
8 localtemp = math.random(1, n)
9
10 localj = 1
11 whilej < i do
12 ifa[j] == temp then
13 break
14 end
15 -- Again damed that There is no ++
16 -- and even no composite operator += !
17 -- No one knows that is more concision
18 -- and more effective?
19 j = j + 1
20 end
21
22 ifj == i then
23 a[i] = temp
24 break
25 end
26
27 end
28 end
29 end
30
31
32 functionrandArr2(a, n)
33 -- use nil as false as a lua's special hack
34 localbt = {}
35 fori=1,n do
36 whiletruedo
37 localtemp = math.random(1,n)
38 ifnotbt[temp] then
39 a[i] = temp
40 bt[temp] = true
41 break
42 end
43 end
44 end
45 end
46
47 functionrandArr3(a, n)
48 fori=1,n do
49 a[i] = i
50 end
51
52 fori=1,n do
53 localtemp = math.random(1,n)
54 -- one of the most things i like in lua&py
55 a[i],a[temp] = a[temp],a[i]
56 end
57 end
58
59
60
61 -- Test Code
62
63 times = 100
64 t = {}
65 randArr1(t, times)
66 fori,v inipairs(t) do
67 io.write(v .. " ")
68 end
69
70
71 t2 = {}
72 randArr2(t2, times)
73 fori,v inipairs(t2) do
74 io.write(v .. " ")
75 end
76
77
78 t3 = {}
79 randArr3(t3, times)
80 fori,v inipairs(t3) do
81 io.write(v .. " ")
82 end
83
84
85



PYTHON:

1 #!/usr/bin/env python
2 fromrandom importrandint
3
4 defrandArr1(a,n):
5 fori inrange(n):
6 whileTrue:
7 temp = randint(0, n-1)
8
9 forj inrange(i):
10 ifa[j] == temp:
11 break;
12 # another one of the things I like in py(for else)
13 else:
14 a.append(temp)
15 break;
16
17 defrandArr2(a,n):
18 #surely it is not need be a dict but dict is faster then list
19 bd = {}
20 fori inrange(n):
21 whileTrue:
22 temp = randint(0, n-1)
23 iftemp notintl:
24 a.append(temp)
25 tl[temp] = True
26 break
27
28 defrandArr3(a,n):
29 a = range(n)
30 fori inrange(n):
31 temp = randint(0, n-1)
32 # like in lua
33 a[i],a[temp] = a[temp],a[i]
34
35 deftest():
36 times = 100
37 l = []
38 randArr1(l, times)
39
40 printl
41 l2 = []
42 randArr1(l2, times)
43 printl2
44
45 l2 = []
46 randArr1(l2, times)
47 printl2
48
49
50
51 if__name__ == '__main__':
52 test()



BASH:

1 #!/usr/bin/env bash
2
3
4 # just for a range rand....let me die....
5 # what I had said? bash is not a good
6 # language for describing algorithms
7 randInt()
8 {
9 locala=$1
10 localb=$2
11
12 if((a >b ))
13 then
14 localtemp
15 ((temp =a))
16 ((a =b))
17 ((b =temp))
18 fi
19
20 localmi
21 ((mi =b - a + 1))
22 localr=$((RANDOM%${mi}+${a}))
23 echo-n $r
24 }
25
26 randArr1()
27 {
28 # only one argument because I hate the
29 # bash's indirect reference
30 # you can reference the (4) binarySearch to
31 # see what I said
32 localn=$1
33 declare-aa
34 for((i=1;i<=n;++i ))
35 do
36 whiletrue
37 do
38 temp=`randInt 1$n`
39 j=1
40 while((j<i ))
41 do
42 if((a[j]==temp))
43 then
44 break
45 fi
46 ((++j ))
47 done
48 if((j ==i))
49 then
50 ((a[j]=temp))
51 break
52 fi
53 done
54 done
55
56 echo${a[*]}
57 }
58
59 randArr2()
60 {
61 localn=$1
62 declare-aa
63 # used for bool array
64 declare-ab
65 for((i=1;i<=n;++i ))
66 do
67 whiletrue
68 do
69 localtemp=`randInt 1$n`
70 if[-z${b[temp]}]
71 then
72 ((a[i]=temp))
73 ((b[temp]=true))
74 break
75 fi
76 done
77 done
78
79 echo${a[*]}
80 }
81
82 randArr3()
83 {
84 localn=$1
85 for((i=1;i<=n;++i ))
86 do
87 ((a[i]=i ))
88 done
89 for((i=1;i<=n;++i ))
90 do
91 localtemp=`randInt 1$n`
92 ((t =a[i]))
93 ((a[i]=a[temp]))
94 ((a[temp]=t ))
95 done
96
97 echo${a[*]}
98 }
99
100
101 # so slow that I can't bear it run 100 times
102 randArr1 10
103 randArr2 10
104 randArr3 10
105

write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics