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

来源:互联网 发布:js控制打印纸张大小 编辑:程序博客网 时间:2024/06/12 01:07
 -- /* 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;}-->

  一个无聊男人的疯狂《数据结构与算法分析-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 using namespace std;
  7
  8 int randInt(int i, int j)
  9 {
 10     if(i > j)
 11     {
 12         int temp = i;
 13         i= j;
 14         j= temp;
 15     }
 16
 17     return rand() % (j-i) + i;
 18 }
 19
 20 void randArr1(int arr[], int n)
 21 {
 22     for(int i=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             int temp = randInt(0, n);
 30             int j = 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 void randArr2(int a[], int n)
 49 {
 50     bool *lpb = new bool[n];
 51     memset(lpb,0, n);
 52
 53     for(int i=0; i<n; ++i)
 54     {
 55         while(true)
 56         {
 57             int temp = randInt(0, n);
 58             if(!lpb[temp])
 59             {
 60                 a[i]= temp;
 61                 lpb[temp]= true;
 62                 break;
 63             }
 64         }
 65
 66     }
 67
 68     delete lpb;
 69     lpb = NULL;
 70 }
 71
 72 void swap(int& a, int&b)
 73 {
 74     int temp = a;
 75     a = b;
 76     b = temp;
 77 }
 78
 79 // justfor fun,but I like it.
 80 // when pycould have range and bash could have seq
 81 // why cppcouldn't have a seq like this? lol
 82 template<typename T>
 83 class CFBSeq
 84 {
 85 public:
 86     CFBSeq(const T& aValue) : mValue(aValue) {}
 87
 88     T operator()()
 89     {
 90         return mValue++;
 91     }
 92
 93 private:
 94     T mValue;
 95 };
 96
 97 void randArr3(int a[], int n)
 98 {
 99     generate(a,a+n, CFBSeq<int>(0));
100
101     for(int i=1; i<n; ++i)
102     {
103         swap(a[i],a[randInt(0,i)]);
104     }
105
106 }
107
108
109
110
111
112 int main(int argc, char*argv[])
113 {
114     const int TIMES=100;
115     srand(time(NULL));
116     int a[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/envlua
 2
 3 math.randomseed(os.time())
 4
 5 function randArr1(a,n)
 6     for i=1,ndo
 7         while true do
 8             local temp = math.random(1, n)
 9
10             local j = 1
11             while j < i do
12                 if a[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             if j == i then
23                 a[i]= temp
24                 break
25             end
26
27         end
28     end
29 end
30
31
32 function randArr2(a,n)
33     -- use nil as false as a lua's special hack
34     local bt = {}
35     for i=1,ndo
36         while true do
37             local temp = math.random(1,n)
38             if not bt[temp]then
39                 a[i]= temp
40                 bt[temp]= true
41                 break
42             end
43         end
44     end
45 end
46
47 function randArr3(a,n)
48     for i=1,ndo
49         a[i]= i
50     end
51     
52     for i=1,ndo
53         local temp = 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 for i,v in ipairs(t)do
67     io.write(v .. "")
68 end
69
70
71 t2 = {}
72 randArr2(t2, times)
73 for i,v in ipairs(t2)do
74     io.write(v .. "")
75 end
76
77
78 t3 = {}
79 randArr3(t3, times)
80 for i,v in ipairs(t3)do
81     io.write(v .. "")
82 end
83
84
85



PYTHON:

 1#!/usr/bin/envpython
 2 from randomimport randint
 3
 4 def randArr1(a,n):
 5     for i in range(n):
 6         while True:
 7             temp= randint(0, n-1)
 8
 9             for j in range(i):
10                 if a[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 def randArr2(a,n):
18 #surely it isnot need be a dict but dict is faster then list
19     bd = {}
20     for i in range(n):
21         while True:
22             temp= randint(0, n-1)
23             if temp not in tl:
24                 a.append(temp)
25                 tl[temp]= True
26                 break
27
28 def randArr3(a,n):
29      a = range(n)
30      for i in range(n):
31         temp = randint(0, n-1)
32         # like in lua
33         a[i],a[temp] = a[temp],a[i]
34
35 def test():
36     times = 100
37     l = []
38     randArr1(l,times)
39
40     print l
41     l2 = []
42     randArr1(l2,times)
43     print l2
44
45     l2 = []
46     randArr1(l2,times)
47     print l2
48
49
50
51 if __name__ == '__main__':
52     test()



BASH:

  1#!/usr/bin/envbash
  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     local a=$1
 10     local b=$2
 11
 12     if (( a> b ))
 13     then
 14         local temp
 15         (( temp = a ))
 16         (( a = b ))
 17         (( b = temp ))
 18     fi
 19
 20     local mi
 21     (( mi = b- a + 1))
 22     local r=$((RANDOM%${mi}+${a}))
 23     echo -n $r
 24 }
 25
 26 randArr1()
 27 {
 28 # only oneargument because I hate the
 29 # bash'sindirect reference
 30 # you canreference the (4) binarySearch to
 31 # see whatI said
 32     local n=$1
 33     declare -a a
 34     for (( i=1; i<=n; ++i )) 
 35     do
 36         while true
 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     local n=$1
 62     declare -a a
 63     # used for bool array
 64     declare -a b
 65     for (( i=1; i<=n; ++i ))
 66     do
 67         while true
 68         do
 69             local temp=`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     local n=$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         local temp=`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 thatI can't bear it run 100 times
102 randArr1 10
103 randArr2 10
104 randArr3 10
105

 

 

 

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