Chapter 1 data structure and algorithms
-
Upload
tha-phanit -
Category
Technology
-
view
264 -
download
4
description
Transcript of Chapter 1 data structure and algorithms
Data Structure And Algorithms
1-1 : esckþIepþIm ³emeronenHENnaMeGayEsVgyl;GMBIbBaða Data Structure and
Algorithms nigGMBItYnaTIehIyTMnak;TMngrbs;vaenAkñúgkmµviFI nigkarEsVgyl;GMBI Structure
Langluage Edl)anykmkeRbIsMrab;Gnuvtþn_elIStructure rbs; Algorithms .
ral;cMeNaTbBaðaEdlRtUvedaHRsayCamUldæan Machine caM)ac;RtUvEsVgyl;GMBI Data Structure and
Algorithms Camunsin BIeRBaH Data Structure and Algorithms
CabBaðamUldæanrbs; Programming . eyIgman Structure Edl)anbegáIteLIgedayelak Nicklaus Wirth (Swiss computer scientist) :
Data Structure + Algorithms = program
mann½yfa ³cMeNaTbBaða rkdMeNaHRsay nig erobcMrebobedaHRsay sresrkmµviFI
1
* Program ³ គឺ�ជាសំ�នុំ��មួយសំ�រាប់ ផ្ទុ��កនុំ�វរាល់ គឺ�នុំ�តយោ�ប់ល់ រប់សំ អ្ន�កសំរយោសំរកមួ�វ �ធី� យោ តាមួប្រប់ព័$នុំ%ក�ណត
រប់សំ ម៉ា( សំ��នុំដែ*ល់យោប្រ+យព័�+រអ្ននុំ�វត,យោ តាមួ*�យោណ� រ+រ process រប់សំ ម៉ា( សំ��នុំ យោយ�ងទទល់បានុំល់ទ%ផ្ទុល់ដែ*ល់ចង បានុំ ។
Algorithms = Logic + Control
* Data Structure : គឺ�ជា+រយោរ2ប់ច� Data រប់សំ អ្ន�កសំរយោសំរ program យោ តាមួ code រប់សំ ម៉ា( សំ��នុំ
នុំ�ងរាល់ data ដែ*ល់យោយ�ងទាញយកមួកយោប្រប់�ប្របាសំ វ �ញក��ងយោ5ល់ប់�ណងអ្ន6�មួយ ។
- Logic គឺ�ជាច�យោណ7*8ងមួ�ល់ដ្ឋា: នុំយោប្រប់�សំ�រាប់ ក�នុំត អ្នត;នុំ$យយោ<យ Algorithms ដែផ្ទុ�ក Logic ជាអ្ន�ក*8ង
Algorithms ប្រត=វយោធី6� ។
- Control គឺ�ជាវ�ធី�សាប្រសំ,យោដ្ឋា7ប្រសាយយោល់�ដែផ្ទុ�ក Logic វាយោ<យ*8ងថា Algorithms ប្រត=វយោធី6�*�ចយោមួ,ច ។
ឧទាហរណC : ច�រយោល់�កយោD�ងអ្ន�ព័�ល់កEណF Logic and Control រប់សំ ច�យោGទប់ញ្ហាI n! ។
យោយ�ងបានុំ :
Logic រប់សំ ប់ញ្ហាI n! = 1 x 2 x 3 ….. X n
Control រប់សំ n! យោល់�ដែផ្ទុ�ក Logic យោនុំ7រ មួម៉ានុំ 3 រយោប់2ប់ :
* Algorithms : គឺ�ជាជំ�ហានុំនៃនុំ+រយោដ្ឋា7ប្រសាយប់ញ្ហាI យោ តាមួ*�Gក +ល់ៗជា Flowchart នុំ�ង មួNOងយោទ2ត
Algorithms គឺ�ជាដែPQ statement *RចSសំ លាសំ ដែ*ល់យោប្រ+យព័�+រក�នុំត ជាក លាក យោយ�ងទទល់បានុំ ល់ទ%ផ្ទុល់ដែ*ល់យោយ�ងចង បានុំ ។
យោយ�ងម៉ានុំ Structure ទ�យោ សំ�រាប់ ដែសំ6ងយល់ ប់ញ្ហាI Algorithms ប់ដែនុំ;មួយោទ2ត ដែ*ល់ ប្រត=វបានុំប់យោងU�តយោD�ងយោដ្ឋាយយោលាក Kowalski
ដែ*ល់ជាអ្ន�កប់យោងU�តកមួ�វ �ធី� Logic ប្រត=វបានុំសំរយោសំរ :
2
របៀ���ទី�១ : គឺ�ណប់ប្រងVមួប់G, យោល់Pព័� 1 *ល់ n ដ្ឋាក ច�ល់ក��ងអ្នយោWរGមួយ
f = 1; for ( i=1 ; i <= n ; i++ )
f = f * i;
f = 1; for ( i = n ; i >= 1 ; i-- )
f = f * i;
របៀ���ទី�២ :គឺ�ណប់ប្រងVមួប់G, យោល់Pព័� n *ល់ 1 ដ្ឋាក ច�ល់ក��ងអ្នយោWរGមួយ
របៀ���ទី�៣ :
គឺ�ណប់ប្រងVមួប់G, យោល់Pយោសំសំជាមួយ5� ដ្ឋាក ច�ល់ក��ងអ្នយោWរGមួយ នុំ�ង គឺ�ណប់ប្រងVមួប់G, យោល់Pគឺ� ជាមួយ5� ដ្ឋាក
ច�ល់ក��ងអ្នយោWរGមួយយោទ2ត ប់ន្ទា� ប់ មួកគឺ�ណល់ទ%ផ្ទុល់ទា�ងព័�រជាមួយ5� ដ្ឋាក ក��ងអ្នយោWរGមួយ ។
3
1-2- ប់ញ្ហាI ទ�ន្ទាក ទ�នុំងមួយច�នុំនុំរប់សំ Data Structure
- Data structure សំ�ដែ*ង+រ Process យោZក��ង Memory យោ<យយោ[� 7ថា Storage structure ។
-Predefine Data structure គឺ�ជា Structure ដែ*ល់ម៉ានុំប្រសាប់ យោZក��ងភាសាកមួ�វ �ធី�*�ចជា Array Structure ។
+រយោប្រជំ�សំយោរ ]សំ Predefine Data Structure សំមួប្រសំប់សំ�រាប់ *�យោG7ប្រសាយប់ញ្ហាI ន្ទា�យោ<យម៉ានុំ+រ Process Algorithms
ម៉ានុំយោល់^_នុំយោល់`នុំ នុំ�ងផ្ទុ,ល់ ល់ទ%ផ្ទុល់ប្រត8មួប្រត=វ ។
- រាល់ ច�យោGទប់ញ្ហាI ដែ*ល់ម៉ានុំប្រទង ប្រទាយត�ចម៉ានុំប់រ �ម៉ាណ Data ត�ច នុំ�ង រាល់ +រយោធី6�ប្រប់ម៉ាណវ�ធី� រប់សំ វាប្រ5នុំ ដែតយោប្រប់�
ប្រប់ម៉ាណវ�ធី� + , - , * , / នុំ�ង logic យោ<យយោ[� 7ថា ប់ញ្ហាI ប់យោចaកយោទសំ ។
- រាល់ ច�យោGទប់ញ្ហាI ដែ*ល់ម៉ានុំប្រទង ប្រទាយធី�ម៉ានុំប់រ �ម៉ាណ Data យោប្រច�នុំយោហ�យរាល់ +រយោធី6�ប្រប់ម៉ាណវ�ធី� រប់សំ វាមួ�នុំប្រ5នុំ ដែត
យោប្រប់�នុំ�វប្រប់ម៉ាណវ�ធី� + , - , * , / នុំ�ង Logic ប់(�យោGb 7យោទ គឺ�ប្រត=វបានុំអ្ននុំ�វត,ជាមួយប្រប់ម៉ាណវ�ធី�W��មួយ យោទ2ត *�ចជា ប់យោងU�នុំ , ប់ដែនុំ;មួ ,
+ត ប់នុំ;យ , ល់�ប់ , ដែសំ6ងរក នុំ�ងត�យោរ2ប់ជាយោ*�មួយោ<យយោ[� 7ថា ច�យោGទប់ញ្ហាI : Non Numerical Problem ។
1-3- តន្ទាទ� នុំ�ងទ�ន្ទាក ទ�នុំង5� រវាង Data Structure and Algorithms
យោZយោព័ល់យោដ្ឋា7ប្រសាយជាមួយ Machine ជា*�ប់�ងយោយ�ងប្រត=វគឺ�ត*ល់ ប់ញ្ហាI Data Structure and Algorithm រប់សំ
ប់ញ្ហាI យោន្ទា7 ។
Algorithms ម៉ានុំតន្ទាទ�យោធី6�+រ process រប់សំ ប់ញ្ហាI រ ]ឯ Data Structure ម៉ានុំតន្ទាទ�យោធី6�+រ process ជាមួយ
Machine យោ*�មួ �̂ន្ទា�*ល់ ល់ទ%ផ្ទុល់ចង បានុំរប់សំ ច�យោGទប់ញ្ហាI ។
ទ�ន្ទាក ទ�នុំងរវាង Data Structure and Algorithms គឺ�ម៉ានុំទ�ន្ទាក ទ�នុំង5� �( ងជំ�តសំ��ទ%ជាមួយ5� ម៉ានុំនុំ$យថា
យោZយោព័ល់យោយ�ងនុំ��យ*ល់ ប់ញ្ហាI Data ប្រត=វគឺ�តថា Data យោន្ទា7បានុំជំ7ឥទ%�ព័ល់*ល់ Algorithms Gមួយ
4
យោហ�យយោZយោព័ល់យោយ�ងនុំ��យ*ល់ ប់ញ្ហាI Algorithms ប្រត=វ*8ងថា Algorithms យោន្ទា7បានុំជំ7ឥទ%�ព័ល់យោល់� Data G យោ*�មួ �̂ន្ទា�*ល់
ល់ទ%ផ្ទុល់ចង បានុំ ។
ឧទាហរណC : ច�រយោល់�កយោD�ងព័�តន្ទាទ�រប់សំ Data Structure and Algorithms យោZក��ងច�យោGទប់ញ្ហាI
ax2 + bx + c = 0
- Algorithms
+ ព័�ភាកeយោល់�យោមួគឺ�ណ a
+ សំ�ដែ*ង : delta = b * b – 4 * a * c
+ ព័�ភាកeយោល់�សំញ្ហាf delta
- if delta < 0 No root
- if delta = 0 X1 = X2 = -b / 2a
- if delta > 0
យោប់� a = 0 យោន្ទា7 bx + c = 0
យោប់� a ≠ 0 យោន្ទា7 ax2 + bx + c =0
X1 =
X2 =
- b - sqrt(delta)
2a
- b + sqrt(delta)
2a
5
- Data Structure
+ +រយោរ2ប់ច�រចន្ទាកមួ�វ �ធី�
+ +រប្រប់+សំអ្នយោWរ : a ; b ; c ; X1 ; X2 ; delta
+ ប់ញ្ចូa�ល់អ្នយោWរចាំ�បាច រប់សំ ច�យោGទ a ; b ; c
+ ប់ង្ហាI ញយោចញល់ទ%ផ្ទុល់រប់សំ ច�យោGទ
1-4- ភាសាសំ�រាប់ Algorithms
យោZយោព័ល់យោដ្ឋា7ប្រសាយជាមួយ Machine យោយ�ងចាំ�បាច ប្រត=វគឺ�ត*ល់ ប់ញ្ហាI Algorithms and Structure យោហ�យយោZយោព័ល់
សំ�ដែ*ងយោចញ structure រប់សំ Algorithms and data structure យោយ�ងប្រត=វដ្ឋាក យោចញនុំ�វ language រប់សំ Algorithms and
Data Structure រប់សំ ច�យោGទប់ញ្ហាI យោ<យម៉ានុំល់កEណFង្ហាយប្រសំVល់ក��ង+រដែសំ6ងយល់ នុំ�ង ង្ហាយសំរយោសំរយោយ�ងគឺរយោប្រជំ�សំយោរ ]សំនុំ�វភាសា
កមួ�វ �ធី�Gមួយ ដែ*ល់ម៉ានុំល់កEណFជាមួ�ល់ដ្ឋា: នុំ យោហ�យម៉ានុំប្រគឺប់ ល់ទ%ភាព័<ចយោធី6�+រឆ្លុះk�7ប់ញ្ហាa �ងរាល់ Structure ដែ*ល់យោល់�កយោD�ង*�ចជា
pascal , c/c++ , Java programming ។
*�ចយោនុំ7 ក��ងឯកសារយោនុំ7 យោយ�ងយោប្រជំ�សំយោរ ]សំយក C Programming Language សំ�រាប់ យោចញនុំ�វរាល់ Structure រប់សំ
Algorithms ជាមួ�ល់ដ្ឋា: នុំមួយច�នុំនុំរប់សំ C Programming Language *�ចខាងយោប្រ+មួ :
i- Structure C Programming Language
- Header file- Gobble variable- Function- void main () { …..……. }
6
ii- Assignment Statement
V = E ; ( V = variable , E = Expression )
- អ្នយោWរ E ជាអ្ន�កផ្ទុ,ល់ ត�នៃល់យោ<យអ្នយោWរ V
- អ្នយោWរ V ជា Expression , Variable , Value
X = Y ; X = 20 ; X = Y + 10 ; Y = X
iii- Data type គឺ�ជាប្រប់យោmទ Data យោប្រប់�សំមួរាប់ ប្រប់+សំប្របាប់ ព័�ល់កEណFអ្នយោWរ ។
Data type ម៉ានុំ ២ ប្រប់យោmទគឺ� :
(1) Simple data type : int , float , char
(2) Structure data type : Array , String , Pointer , Enum , Struct , Union and File.
iv- Statement
1- Control Statement
- if (condition) statements;- if (condition) statement1 ;else statement2 ; statment3;
Condition
Statement 2 Statement 1
False True
7
Condition
Statement 2 Statement 1
False True
Statement 3
8
Condition 1
Statement 1
True False
Condition 2
Statement 2Condition 3
Statement 3 Statement 4
True False
True False
- if (condition 1) statement 1;else if (condition 2) statement 2 ; ……………..else statement(n);
9
- Nested if () statement
- if (condition ) statement 1; else { if ( condition )
statement 2;else { statement 3; statement 4; statement 5; }
}
- Switch ( expression ){Case const1 : statement1 ; break ;Case const2 : statement2 ; break ;………………………………………Default : statement n ;}
10
2- Loop Statement
• For Loop
for ( exp 1 ; exp 2 ; exp 3 ) statement ;
Exp 1 ; exp 2 ; exp 3
Statement 1
Expression 2
Statement
• While Loop
expression 1 ;while (expression 2) { statement ; expression 3 ; }
Statement 3
True
False
Expression 1
11
True
False
• do / while loop : exp 1 ; do { statement ; exp3 ; } while
(exp2);
exp 1
statement
exp 3
exp 2True
FalseឧទាហរណC ១:
program to illustrate the character checking, Accept choice and print appropriate message
#include <stdio.h>
main()
{
char ch;
printf(“Enter your choice y/n “);
scanf(“%c”,&ch);
if ( ch = = ‘ y ‘ || ch = = ‘ Y ‘ )
printf (“Your choice is yes \ n ”);
else
printf (“Your choice is no \ n “);
} /* main end */12
ឧទាហរណC ២:
សំរយោសំរកមួ�វ �ធី�យោ*�មួ �̂ប្រតVតព័�នុំ�តpច�នុំនុំប់ឋមួ រ r មួ�នុំប់ឋមួ( A prime number is one which is divisible only by 1 or itself )
#include <stdio.h>
#include <conio.h>
main()
{
clrscr();
int num,i;
printf("Enter a number :");
scanf("%d",&num);
i=2;
while(i<=num -1)
{
if(num %i==0)
{
printf("%i is not a prime",num);
break;
}
i+=1;
}
if(i==num)
printf("%i is Prime number",num);
getch();
} 13
ឧទាហរណC ៣:
សំរយោសំរកមួ�វ �ធី�យោ*�មួ �̂គឺណន្ទាផ្ទុល់ប់�ក Count ច�នុំនុំវ �ជំsម៉ានុំ : 1 + 2 + 3 + … + count
#include <stdio.h>
#include <conio.h>
main()
{
long sum=0;
int i=1;
int count=0;
clrscr();
printf("Enter the number of integers you want to sum :");
scanf("%d",&count);
while(i<=count)
{
sum=sum+i;
++i;
}
printf("\n Total of the first %d number is %d \n,count,sum);
getch();
}
14
ឧទាហរណC ៤:
សំរយោសំរកមួ�វ �ធី�យោ*�មួ �̂គឺណន្ទាផ្ទុល់ប់�ក ផ្ទុល់*កនុំ�ងផ្ទុល់គឺ�ណយោដ្ឋាយយោប្រប់� Switch Statement
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,sum,diff,pro,ch;
float rat;
clrscr();
printf("Enter value of a and b: \n");
scanf("%d %d",&a,&b);
printf("\n M E N U");
printf("\n 1 for Sum");
printf("\n 2 for Product");
printf("\n 3 for Ratio");
printf("\n 4 for Difference");
printf("\n Enter your choice 1 / 2 / 3 / 4:");
scanf("%d",&ch);
switch (ch){
case 1 :
sum=a+b;
printf("Sum of a & b=%d",sum);
break;
case 2 :
pro=a*b; 15
printf("Product of a & b=%d",pro);
break;
case 3 :
rat=a/b;
printf("Ratio of a & b=%f",rat);
break;
case 4 :
diff=a-b;
printf("Diff of a & b=%d",diff);
break;
}
getch(); }
v- Function :
Structure of Function
Return type name function (list parameter ………){ Local variable statement; …………..}
16
ក��ងយោន្ទា7 return type រ មួម៉ានុំ ២ប្រប់យោmទ គឺ� void នុំ�ង data type
* Name function ជាយោ[� 7 function
* ប់G, parameter យោប្រប់�យោZយោព័ល់ Declaration Function យោuថា parameter ។
* ប់G, parameter ដែ*ល់ផ្ទុ,ល់ យោ<យ Function (ក��ង main program) យោZយោព័ល់យោយ�ងយោu Function យោuថា
Argument ។
* List Parameter រ មួម៉ានុំ ២ប្រប់យោmទគឺ� :
- Value parameter គឺ�ជា parameter ដែ*ល់មួ�នុំដែប្រប់ប្រប់Vល់ត�នៃល់មួ�នុំយោព័ល់ Call function នុំ�ងយោប្រ+យយោព័ល់ Call
function ។
- Reference parameter គឺ�ជា parameter ដែ*ល់ដែប្រប់ប្រប់Vល់ត�នៃល់មួ�នុំយោព័ល់ Call function នុំ�ងយោប្រ+យយោព័ល់ Call
function ។Example 1 : // passing parameters by reference
#include <stdio.h>
#include <conio.h>
void duplicate(int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
getch();
}
int main()
{ 17
18
int x=1,y=3,z=7;
duplicate(x,y,z);
printf("x=%d y=%d z=%d",x,y,z);
return 0;
}
Result : x=2, y=6, z=14
Example 2 : // more than one returning value
#include <stdio.h>
#include <conio.h>
void prevnext(int x, int& prev, int& next)
{
prev=x-1;
next=x+1;
getch();
}
int main()
{
int x=100,y,z;
prevnext(x,y,z);
printf("x=%d, Previous=%d, Next=%d",x,y,z);
return 0;
}
Result : X=100, Previous=99, Next=101
* រយោប់2ប់សំរយោសំរអ្ននុំ�គឺមួនុំC ម៉ានុំ ៤ដែប់ប់ដែ*ល់យោយ�ងប្រត=វបានុំយោល់�កយោD�ងគឺ� :ឧទាហរណC : គឺណន្ទាផ្ទុល់ប់�ក ២ច�នុំនុំគឺត a នុំ�ង b
១- អ្ននុំ�គឺមួនុំCម៉ានុំ data type and ម៉ានុំ parameter
int sum1( int a, int b )
{
int s= a + b ;
return (s);
}
ឧទាហរណC : Value parameter
void change (int x , int y); /* x and y ជា value parameter */
ឧទាហរណC : Reference parameter
void change (int& x , int& y); /* x and y ជា reference parameter */
២- អ្ននុំ�គឺមួនុំCម៉ានុំ data type and 5� នុំ parameter
int sum2()
{
int a,b;
printf("input a , b : ");
scanf("%d %d",&a,&b);
int s=a+b;
return(s);
}
19
20
៣- អ្ននុំ�គឺមួនុំC5� នុំ data type and ម៉ានុំ parameter
void sum3( int a , int b)
{
int s=a+b;
printf(“\n sum = %d “,s);
}
៤- អ្ននុំ�គឺមួនុំC5� នុំ data type and 5� នុំ parameter
void sum4()
{
int a,b;
printf("input a,b :");
scanf("%d %d ",&a,&b);
int s=a+b;
printf("\n sum=%d",s);
}
Storage Class
A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.
There are following storage classes which can be used in a C Program
auto
register
static
Extern
21
auto - Storage Class
auto is the default storage class for all local variables.
{
int Count;
auto int Month;
}
The example above defines two variables with the same storage class. auto can only be used within functions, i.e.
local variables.
register - Storage Class
register is used to define local variables that should be stored in a register instead of RAM. This means that the
variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator
applied to it (as it does not have a memory location).
{
register int Miles;
}
Register should only be used for variables that require quick access - such as counters. It should also be noted that
defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a
register - depending on hardware and implimentation restrictions.
static - Storage Class
static is the default storage class for global variables. The two variables below (count and road) both have a static
22
storage class.
static int Count;
int Road;
{
printf("%d\n", Road);
}
extern - Storage Class
extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern'
the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously
defined.
When you have multiple files and you define a global variable or function which will be used in other files also, then
extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.