Data Structures: 1. Arrays
Data Structures: 1. Arrays
Data Structures: 1. Arrays
1. Arrays
ﺗﻌﺪ اﻟﻤﺼﻔﻮﻓﺎت ﻣﻦ اﻟﻬﻴﺎآﻞ اﻟﺒﻴﺎﻧﻴﺔ اﻟﺒﺴﻴﻄﺔ اﻟﺘﺮآﻴﺐ واﻻﺳﺘﺨﺪام وهﻲ ﺗﺴﺘﺨﺪم ﻣﻦ ﻗﺒﻞ اﻟﻤﺒﺮﻣﺞ ﺑﺒﺴﺎﻃﺔ دون اﻟﺤﺎﺟﺔ اﻟﻰ ﻣﻌﺮﻓﺔ ﺗﻘﻨﻴﺔ ﺧﺰﻧﻬﺎ
ﺑﺎﻟﺬاآﺮة وذﻟﻚ ﺑﺎﺳﺘﺨﺪام اﻟﻤﻌﺎدﻻت اﻟﺮﻳﺎﺿﻴﺔ.
اذن-:
اﻟﻤﺼﻔﻮﻓﺔ هﻲ ﻋﺒﺎرة ﻋﻦ ﻣﺠﻤﻤﻮﻋﺔ ﻣﻦ اﻟﻤﻮاﻗﻊ اﻟﺨﺰﻧﻴﺔ ﻓﻲ اﻟﺬاآﺮة او هﻲ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻻزواج)اﻟﻘﻴﻢ و اﻟﻤﻮﻗﻊ( واﻟﻌﻤﻠﻴﺎت اﻟﺘﻲ ﺗﺤﺼﻞ ﻋﻠﻴﻬﺎ
هﻲ-:
1. Create empty array
2. Store a partial mapping from index to value
3. Retrieve use that mapping
ﻓﻮاﺋﺪهﺎ-:
.1ﺳﻬﻠﺔ اﻻﺳﺘﺨﺪام
.2ﺳﻬﻠﺔ اﻟﺘﻨﻔﻴﺬ
.3اﻻﺣﺘﻔﺎظ ﺑﺎآﺜﺮ ﻣﻦ ﻗﻴﻤﺔ ﻟﻠﻤﺘﻐﻴﺮ
.4ﺗﺴﻬﻴﻞ ﺑﻌﺾ اﻟﻤﻌﺎﻟﺠﺎت اﻟﻤﻌﻘﺪة
ﻣﺴﺎوﺋﻬﺎ-:
.1اﻟﻮﺻﻮل ﻓﻴﻬﺎ ﻋﺸﻮاﺋﻲ
.2ﻻﻳﻤﻜﻦ ﺣﺸﺮ او ﺣﺬف اي ﻣﻮﻗﻊ
.3ﻻﻳﻤﻜﻦ ان ﻧﺨﺰن ﻓﻴﻬﺎ ﺳﻮى ﻧﻮﻋﻴﺔ واﺣﺪة
.4اﻟﺨﺰن ﻓﻴﻬﺎ ﻏﻴﺮ دﻳﻨﺎﻣﻴﻜﻲ
واﻟﻤﻌﺎدﻟﺔ اﻟﻌﺎﻣﺔ اﻟﺘﻲ ﻳﺘﺤﺪد ﺑﻮاﺳﻄﺘﻬﺎ ﻋﻨﻮان اي ﻋﻨﺼﺮ ﻓﻲ اﻟﺬاآﺮة ﺑﻤﺼﻔﻮﻓﺔ اﺣﺎدﻳﺔ هﻲ-:
1
Loc (Name [I]) = Loc (Name [1]) + [I-1]* w
ﺣﻴﺚ-:
Nameهﻮ اﺳﻢ اﻟﻤﺼﻔﻮﻓﺔ
)] Loc (Name [Iهﻮ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ
)] Loc (Name [1هﻮ ﻣﻮﻗﻊ اول ﻋﻨﺼﺮ ﻣﺼﻔﻮﻓﺔ ﻓﻲ اﻟﺬاآﺮة
Wهﻲ ﻋﺪد ارﻗﺎم اﻟﻜﻠﻤﺎت اﻟﻤﺤﺪدة ﻟﻜﻞ ﻋﻨﺼﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ وﻓﻲ ﺑﻌﺾ اﻻﺣﻴﺎن ﻻﺗﺬآﺮ ﻓﺘﻌﺘﺒﺮ )واﺣﺪ( او ﺗﻬﻤﻞ
] A [3ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﺗﺤﺘﻮي ﻋﻠﻰ ﺳﺒﻌﺔ ﻣﻮاﻗﻊ ﻋﻠﻤًﺎ ان اﻟﻌﻨﺼﺮ اﻷول ﻟﻠﻤﺼﻔﻮﻓﺔ ﻣﺜﺎل-:اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ
ﻣﺨﺰون ﻓﻲ اﻟﻤﻮﻗﻊ200
2
اﻟﻬﻴﻜﻞ اﻟﺘﺨﺰﻳﻨﻲ )اﻟﺬاآﺮة(-:
ان ﻋﻤﻠﻴﺔ ﺗﺤﻮﻳﻞ اﻟﺒﻴﺎﻧﺎت ﻣﻦ اﻟﻬﻴﻜﻞ اﻟﻤﻨﻄﻘﻲ اﻟﻰ اﻟﻬﻴﻜﻞ اﻟﺘﺨﺰﻳﻨﻲ ﻓﻲ اﻟﺬاآﺮة ﺗﺘﻢ ﻳﻄﺮﻳﻘﺘﻴﻦ-:
1. Row Wise
2. Column Wise
اﻣﺎ ﻓﻲ ﻟﻐﺔ ﺑﺎﺳﻜﺎل ﻓﻴﺘﻢ اﻟﺨﺰن ﻓﻴﻬﺎﺑﺎﻟﺬاآﺮة ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ اﻟﺼﻔﻮف اﻣﺎ ﻟﻐﺔ ﺑﻴﺴﻚ وآﻮﺑﻞ وﻓﻮرﺗﺮان ﻓﻴﺘﻢ اﻟﺨﺰن ﻓﻴﻬﺎ ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ
ﺧﺰن اﻻﻋﻤﺪة وذﻟﻚ ﺣﺴﺐ ﺗﺼﻤﻴﻢ ﻣﺘﺮﺟﻢ او ﻣﻔﺴﺮ ﺗﻠﻚ اﻟﻠﻐﺔ.
a11 a12 a13 a21 a22 a23 a31 a32 a33 a41 a42 a43
Row Wise
a11 a21 a31 a41 a12 a22 a32 a42 a13 a23 a33 a43
Colume Wise
Row Wise:-
ﺣﻴﺚ اﻧﻬﺎ ﻃﺮﻳﻘﺔ ﺗﺨﺰﻳﻨﻴﺔ ﻳﺘﻢ ﻓﻴﻬﺎ ﺗﺨﺰﻳﻦ ﺑﻴﺎﻧﺎت اﻟﺼﻒ اﻻول ﻓﻲ اﻟﺬاآﺮة وﻳﻌﺪهﺎ ﺑﻴﺎﻧﺎت اﻟﺼﻒ اﻟﺜﺎﻧﻲ واﻟﺜﺎﻟﺚ وهﻜﺬا.
اﻣﺎ اﻟﻤﻌﺎدﻟﺔ اﻟﺘﻲ ﻧﺤﺘﺎﺟﻬﺎ ﻟﺘﺮﺗﻴﺐ ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ ﻓﻲ اﻟﺬاآﺮة ﻓﻬﻲ -:
3
ﻳﺘﻢ اﺳﺘﺨﺪام هﺬا اﻟﻘﺎﻧﻮن ﻓﻲ ﺣﺎﻟﺔ ﺗﺨﺼﻴﺺ ﻋﺪد ﻣﻦ اﻟﻜﻠﻤﺎت ﻟﻜﻞ ﻋﻨﺼﺮ
] A [3, 4واﻟﻤﻮﻗﻊ اﻻﺑﺘﺪاﺋﻲ ﻟﺨﺰن ﻋﻨﺎﺻﺮهﺎ ﻓﻲ اﻟﺬاآﺮة هﻮ 1500ا ﺣﺴﺐ ﻣﻮﻗﻊ ﻣﺜﺎل اذا آﺎن ﻟﺪﻳﻚ اﻟﻤﺼﻔﻮﻓﺔ
ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ ﺧﺰن اﻟﺼﻔﻮف ]A [2, 3 اﻟﻌﻨﺼﺮ
Column Wise:-
Loc (A [I, J]) = Loc (A [1, 1]) + ([J-1]*N+ [I-1])*W
ﺑﺎﺳﺘﺨﺪام ﻃﺮﻳﻘﺔ ﺧﺰن اﻻﻋﻤﺪة. ]A [3, 2 ] A [9, 3ﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ ﻣﺜﺎل اذا آﺎن ﻟﺪﻳﻚ اﻟﻤﺼﻔﻮﻓﺔ
Loc (A [3, 2]) = Loc (A [1, 1]) + ([J-1]*N+ [I-1])*W
)=1+ (2-1)*9+ (3-1
=1+9+2
=12
] B [5, 6واﻟﻤﻮﻗﻊ اﻻﺑﺘﺪاﺋﻲ ﻟﺨﺰن ﻋﻨﺎﺻﺮهﺎ ﻓﻲ اﻟﺬاآﺮة هﻮ 500وﻋﻠﻰ ﻓﺮض ﺣﺠﺰ ﻣﺜﺎل اذا آﺎن ﻟﺪﻳﻚ اﻟﻤﺼﻔﻮﻓﺔ
ﺑﺎﺳﺘﺨﺪام اﻟﻄﺮﻳﻘﺘﻴﻦ. ]B [2, 3 آﻠﻤﺘﻴﻦ ﻟﻜﻞ ﻋﻨﺼﺮ ﻣﻦ ﻋﻨﺎﺻﺮ اﻟﻤﺼﻔﻮﻓﺔ اﺣﺴﺐ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ
Sol:-
N=5, M=6, I=2, J=3, W=2.
1. Loc (B [2, 3]) = Loc (B [1, 1]) + ([J-1]*N+ [I-1])*W
=500+ ((3-1)*5+ (2-1))*2
=500+11*2
=522
2. Loc (B [2, 3]) = Loc (B [1, 1]) + ([I-1]*M+ [J-1])*W
=500+ ((2-1)*6+ (3-1))*2
=500+8*2
=516
4
ﻣﻼﺣﻈﺎت-:
* ﻓﻲ ﺣﺎﻟﺔ ﻋﺪم ذآﺮ ﻗﻴﻤﺔ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ اﻻﺑﺘﺪاﺋﻲ ﻓﺎﻧﻨﺎ ﻧﻌﺘﺒﺮﻩ واﺣﺪ
* ﻣﺘﺮﺟﻢ او ﻣﻔﺴﺮ اﻟﻠﻐﺔ ﻋﻨﺪﻣﺎ ﻳﺠﺪ ﺗﺼﺮﻳﺤًﺎ ﻟﻤﺼﻔﻮﻓﺔ ﺛﻨﺎﺋﻴﺔ ﻓﺎﻧﻪ ﻳﻘﻮم ﺑﻤﺎ ﻳﺎﺗﻲ-:
وﺗﺒﺪا ﺑﻌﻨﻮان ﻣﻄﻠﻖ ﻓﻲ اﻟﺬاآﺮة ﻳﻘﺎﺑﻞ اﻻﺳﻢ اﻟﻤﻨﻄﻘﻲ اﻟﻤﺼﺮح ﻓﻲ اﻟﺒﺮﻧﺎﻣﺞ N*M .1ﺗﺤﻀﻴﺮ ﻣﻨﻄﻘﺔ ﺧﺰﻧﻴﺔ ﺑﺴﻌﺔ
.2ﻳﺴﺘﺨﺪم ﻣﺘﺮﺟﻢ اﻟﻠﻐﺔ اﻟﻤﻌﺎدﻻت اﻟﺮﻳﺎﺿﻴﺔ اﻟﺘﻲ وﺿﺤﻨﺎهﺎ اﻋﻼﻩ ﻟﺘﺤﺪﻳﺪ ﻋﻨﻮان اي ﻗﻴﻤﺔ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ.
ﻓﻲ ﻣﺼﻔﻮﻓﺔ أﺑﻌﺎدهﺎ 2*3*5ﺑﻄﺮﻳﻘﺘﻲ ﺧﺰن اﻟﺼﻔﻮف واﻷﻋﻤﺪة ]A [2, 2, 3 ﻣﺜﺎل-:اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ
Sol:-
I=2, J=2, K=3, U2=3, U3=5
U1= (I-1)*U2*U3= (2-1)*3*5 =15
)1.Row Wise:- Loc (A [2, 2, 3])=Loc(A[1,1,1])+ (I-1)*U2*U3+(J-1)*U3+(K-1
)=1+15+ (2-1)*5+ (3-1
=1+15+5+2
=23
)2. Column Wise: - Loc (A [2, 2, 3]) = Loc (A [1, 1, 1]) + [I-1]*U2*U3+ [K-1]*U2+ (J-1
)=1+15+ (3-1)*3+ (2-1
= 1+15+6+1=23
5
وﺑﺸﻜﻞ ﻋﺎم ﻳﻤﻜﻦ آﺘﺎﺑﺔ آﻼ اﻟﻘﺎﻧﻮﻧﻴﻦ اﻋﻼﻩ ﺑﺎﻟﺼﻴﻐﺔ اﻟﻌﺎﻣﺔ اﻟﺘﺎﻟﻴﺔ-:
1. Row Wise:-
)Loc (A [I, J, K]) = α+ [I-1]*(U2-L2+1)*(U3-L3+1) + [J-1]*(U3-L3+1) + (K-1
2. Column Wise:-
)Loc (A [I, J, K]) = α+ [I-1]*(U2-L2+1)*(U3-L3+1) + [K-1]*(U2-L2+1) + (J-1
Triangular Matrix
اﻟﻤﺼﻔﻮﻓﺎت اﻟﻤﺜﻠﺜﻴﺔ وهﻲ ﻧﻮﻋﺎن-:
1. Lower Triangular Matrix
2. Upper Triangular Matrix
ﻣﻼﺣﻈﺔ-:
ان ﺗﺨﺰﻳﻦ اﻟﻤﺼﻔﻮﻓﺔ ﺑﺎﻟﺸﻜﻞ اﻟﺜﻨﺎﺋﻲ اﻋﻼﻩ ﻳﻌﻨﻲ ﺣﺠﺰ ﻣﺎ ﻳﻘﺮب ﻣﻦ ﺿﻌﻒ اﻟﻤﺴﺎﺣﺔ اﻟﺘﺨﺰﻳﻨﻴﺔ ﻟﻠﻤﺼﻔﻮﻗﺘﻴﻦ اﻋﻼﻩ ﺑﺎﻟﻘﻴﻤﺔ ﺻﻔﺮ وﺗﻜﻮن
اﻟﺨﺴﺎرة آﺒﻴﺮة ﻟﻮ آﺎﻧﺖ اﻟﻤﺼﻔﻮﻓﺔ اآﺒﺮ ﻣﻦ هﺬا ﻳﺘﻀﺢ اهﻤﻴﺔ هﻴﻜﻠﺔ اﻟﻤﺼﻔﻮﻓﺔ اﻟﻤﺮﺑﻌﺔ واﻋﺘﺒﺎرهﺎ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﻓﻘﻂ.
Lower Triangular Matrix
هﻲ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘﻲ ﺗﻜﻮن ﻓﻴﻬﺎ ﺟﻤﻴﻊ ﻋﻨﺎﺻﺮ اﻟﻤﺤﻮر اﻟﺮﺋﻴﺴﻲ واﻟﻌﻨﺎﺻﺮ اﻟﻮاﻗﻌﺔ أﺳﻔﻞ اﻟﻤﺤﻮر اﻟﺮﺋﻴﺴﻲ ﻗﻴﻢ ﺣﻘﻴﻘﻴﺔ ﻣﺎﻋﺪا ذﻟﻚ ﺗﻜﻮن
أﺻﻔﺎر أي إذا آﺎﻧﺖ ﻗﻴﻤﺔ ﻋﺪاد اﻟﺼﻔﻮف اﺻﻐﺮ ﻣﻦ ﻗﻴﻤﺔ ﻋﺪاد اﻷﻋﻤﺪة ﻓﺎن ﻗﻴﻤﺔ اﻟﻤﻮﻗﻊ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﻳﺴﺎوي ﺻﻔﺮ.
a11 a21 a22 a31 a32 a33 a41 a42 a43 a44
Row Wise
a11 a21 a31 a41 a22 a32 a42 a33 a43 a44 Column Wise
6
Row Wise اﻟﻤﻌﺎدﻟﺔ اﻟﺘﺎﻟﻴﺔ ﺗﺤﺪد ﻣﻮﻗﻊ اي ﻋﻨﺼﺮ ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﺳﻔﻠﻰ ﻋﻨﺪﻣﺎ ﺗﺨﺰن ﻓﻲ اﻟﺬاآﺮة ﺑﻄﺮﻳﻘﺔ اﻟﺼﻔﻮف
ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﺳﻔﻠﻰ أﺑﻌﺎدهﺎ 4*4ﺑﻄﺮﻳﻘﺘﻲ ﺧﺰن اﻟﺼﻔﻮف واﻷﻋﻤﺪة ]A [3, 2 ﻣﺜﺎل-:اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ
• ]Row wise: Loc (A [3, 2]) = Loc (A [1, 1]) + [3-1]*3/2+ [2-1
=1+2*1.5+1
=1+3+1
=5
• ]Column Wise: Loc (A [3, 2]) = Loc (A [1, 1]) + ([2-1]*4- [2-1]*2/2) + [3-1
=1+4-1+2
=6
Upper Triangular Matrix
هﻲ اﻟﻤﺼﻔﻮﻓﺔ اﻟﺘ ﻲ ﺗﻜ ﻮن ﻓﻴﻬ ﺎ ﺟﻤﻴ ﻊ ﻋﻨﺎﺻ ﺮ اﻟﻤﺤ ﻮر اﻟﺮﺋﻴ ﺴﻲ واﻟﻌﻨﺎﺻ ﺮ اﻟﻮاﻗﻌ ﺔ أﻋﻠ ﻰ اﻟﻤﺤ ﻮر اﻟﺮﺋﻴ ﺴﻲ ﻗ ﻴﻢ ﺣﻘﻴﻘﻴ ﺔ ﻣﺎﻋ ﺪا ذﻟ ﻚ ﺗﻜ ﻮن
أﺻﻔﺎر أي إذا آﺎﻧﺖ ﻗﻴﻤﺔ ﻋﺪاد اﻟﺼﻔﻮف اآﺒﺮ ﻣﻦ ﻋﺪاد اﻷﻋﻤﺪة ﻓﺎن ﻗﻴﻤﺔ اﻟﻤﻮﻗﻊ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ ﻳﺴﺎوي ﺻﻔﺮ.
هﻴﻜﻞ ﻣﻨﻄﻘﻲ
a11 a12 a13 a14
0 a22 a23 a24
0 0 a33 a34
0 0 0 a44
a11 a12 a13 a14 a22 a23 a24 a33 a34 a44
Row Wise
a11 a12 a22 a13 a23 a33 a14 a24 a34 a44 Column Wise
اﻣﺎ ﺑﺎﻟﻨﺴﺒﺔ ﻟﻠﻤﺼﻔﻮﻓﺔ اﻟﻤﺜﻠﺜﻴﺔ اﻟﻌﻠﻴﺎ ﻓﺎن اﻟﻤﻌﺎدﻟﺔ اﻟﺘﻲ ﺗﺤﺪد ﻣﻮﻗﻊ اي ﻋﻨﺼﺮﻣﻦ ﻋﻨﺎﺻﺮهﺎ ﻓﻲ اﻟﺬاآﺮة ﺑﻄﺮﻳﻘﺔ ﺗﺨﺰﻳﻦ اﻻﻋﻤﺪة
Column Wise
7
Row Wise
ﻓﻲ ﻣﺼﻔﻮﻓﺔ ﻣﺜﻠﺜﻴﺔ ﻋﻠﻴﺎ أﺑﻌﺎدهﺎ 4*4ﺑﻄﺮﻳﻘﺘﻲ ﺧﺰن اﻟﺼﻔﻮف واﻷﻋﻤﺪة ]A [2, 3 ﻣﺜﺎل-:اوﺟﺪ ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ
• ]Row wise: Loc (A [2, 3])= Loc (A [1, 1]) + ([2-1]*4- [2-1]*2/2) + [3-1
=1+(4-1)+2
=1+3+2
=6
• ]Column Wise: Loc (A [2, 3]) = Loc (A [1, 1]) + [3-1]*3/2) + [2-1
=1+2*1.5+1
=1+3+1
=5
Representation of Arrays
ﻳﺠﺐ ﺗﻨﻔﻴﺬ ﻣﺼﻔﻮﻓﺔ ذات Nﻣﻦ اﻹﺑﻌﺎد ﻓﻲ ذاآﺮة ذات ﺑﻌﺪ واﺣﺪ اذن ﻋﺪد اﻟﻜﻠﻤﺎت اﻟﻤﻄﻠﻮﺑﺔ هﻲ ﺑﻌﺪد اﻟﻌﻨﺎﺻﺮ ﻓﻲ اﻟﻤﺼﻔﻮﻓﺔ .ﻓﺎذا آﺎﻧﺖ
اﻟﻤﺼﻔﻮﻓﺔ ﻣﻌﺮﻓﺔ آﺎﻟﺘﺎﻟﻲ -:
] A [L1...U1, L2...U2, L3...U3, …, Ln…Un
ﻓﺎن ﻋﺪد ﻋﻨﺎﺻﺮهﺎ ﻳﻤﻜﻦ ﻣﻌﺮﻓﺘﻪ ﺣﺴﺐ اﻟﻤﻌﺎدﻟﺔ اﻟﺘﺎﻟﻴﺔ-:
ﺣﻴﺚ-:
nهﻲ ﻋﺪد اﺑﻌﺎد اﻟﻤﺼﻔﻮﻓﺔ
]A [2...5, 6...8 ﻣﺜﺎل -:اوﺟﺪ ﻋﺪد اﻟﻤﻮاﻗﻊ اﻟﻤﻄﻠﻮﺑﺔ ﻟﺨﺰن اﻟﻤﺼﻔﻮﻓﺔ
Μ
2
i =1
(( U 1 − L 1 ) + 1 ) * (( U 2 )− L 2 ) + 1
= (5-2+1)*(8-6+1) = 4*3 =12
8