ASD-1-Algorithmic-dataStructures-1 (1) - 1-68
ASD-1-Algorithmic-dataStructures-1 (1) - 1-68
15 November 2022
Updated December 2022
Plan
2 Arrays
Declaration
Manipulation Arrays
Multi-dimensional Arrays
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Objectives
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Details
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Data Structure ?
Definition
A Data Structure is a Specialized Format for :
Organizing,
Processing,
data.
Retrieving
storing
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Motivation
Problem : let 10 students, for each of them, we want to input and store their grade.
Then compute how many students has the average.
One solution can be expressed by using for each student a separate object (variable)
to store the mark !
Mark1, Mark2, Mark3, Mark4, Mark5, Mark6, Mark7, Mark8, Mark9, and Mark10.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Naive Solution
Algorithm : Success
Data : Mark1, Mark2, Mark3, Mark4, Mark5, Mark6, Mark7, Mark8, Mark9, Mark10 : 10 reals (marks)
Result : NB-success : a Integer that represents the number of succeeded students
/* Declaration */
Mark1, Mark2, Mark3, ... Mark10, success : real ;
success : integer ;
begin
Input(Mark1) ;
Input(Mark2) ;
Input(Mark3) ;
.
.
.
Input(Mark10) ;
success ← 0 ;
if Mark1≥10 then
success ← success +1 ;
endif
if Mark2 ≥ 10 then
success ← success +1 ;
endif
.
.
.
if Mark10 ≥ 10 then
success ← success +1 ;
endif
Output("the number of succeeded Student is", success) ;
end . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Solution
Solution −→ Array
Algorithmic Language (and Programming languages) offer the
possibility of gathering several values in a single data
structure called an array.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Definition
Defintion
An array is a collection of items stored at contiguous location.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Declaration of Array
OR
Examples
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Algorithm : Success-with-array
Data : Mark an array of 10 Integers
Result : NB-success : a Integer that represents the number of succeeded students
/* Declaration of Objects */
Mark : array [10] of real ;
success : integer ;
begin
success ← 0 ;
for i ← 1 to 10 do
Input( Mark[i] ) ;
if Mark[i] ≥ 10 then
success ← success +1 ;
endif
done
Output("The number of succeeded Student is", success) ;
end
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Sorting an Array
Definition
A sorting algorithm puts elements of an array or a list into an
order.
The most frequently used orders are :
numerical : 1 12 23 50
lexicographical "Bird" "Dolphin" "Elephant" "Zebra"
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
/* Declaration of modules */
Action Search ( T : array [100] of integer, e : integer) : integer ;
i : integer ;
Found : boolean ;
begin
Search ← -1 ;
Found ← false ;
while (i ≤ N) and ( not Found) do
if (T[i] = e) then
Search ← i ;
Found ← true ;
endif
i← i+1 ;
done
end
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
end . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
1. From Latinized form of Greek dikhotomia "a cutting in half," from dikho-, combining
.
form of dikha "in two, +
. . . . . . . . . . . . . . . . . . .
temnein "to cut". Also : Dividing into 2 opposed parts . . . . . . . . . . . . . . . . . . . .
Let us searching 4 in A
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
1 Begin with the mid element of the whole array as a search key.
2 If the value of the search key is equal to the sought element then return an index
of the search key.
3 Or if the value of the search key is sought element in the middle of the interval,
narrow the interval to the lower half.
5 Repeatedly check from the second point until the value is found or the interval is
empty.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
/* Declaration of modules */
Action Dichotomy-Search ( T : array [100] of integer, e : integer) : integer ;
i, mid, upper, lower : integer ;
Found : boolean ;
begin
Dichotomy-Search ← -1 ;
Found ← false ;
lower ← 1 ;
upper ← N ;
while (lower ≤ upper) and ( not Found) do
mid ← (upper+lower) div 2 ;
if (T[mid] = e) then
Dichotomy-Search ← mid ;
Found ← true ;
else
if (T[mid] < e) then
lower ← mid+1 ;
else
upper ← mid - 1 ;
endif
endif
done
end
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
2-dimensional Array
2 possible solutions
1 Array of array of Integer
2 Array of 2 dimensions (2-dimensional)
1 1st (Row) Dimension to consider the 80 Students
2 2sd (Column) Dimension to consider the marks of one
student
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
2-dimensional Array
Definition
A 2-dimensional array can be termed as an array of arrays that
stores homogeneous data in tabular form.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Syntax
Examples
In C language
float Mark [ 80 ] [ 6] ;
int Temp [ 30 ] [ 3] ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Access to elements
Input/Output Action :
Input (A[5,4])
Output (A[ i, j])
Assignment Action :
A[ i, j] ← 101 ;
Z ← A[2, 4] * A[2, 6] ; /* figures in an expression
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Local Activity
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
3-dimensional Array
Example of deployment
In order to store the temperatures : 3 by day, for all days of the month and for all
months of the year.
Temp : array [ 3 , 31, 12 ] ) of integer ;
In C language
. . . . . . . . . . . . . . . . . . . .
int Temp [ 3 ] [ 31] [ 12] ; . . . . . . . . . . . . . . . . . . . .
k -dimensional Array
of Type-elements ;
Example of deployment
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Syntax
My_answer char ;
Djawab char ;
Usage
My_answer ← ’O’ ;
Djawab ← ’Y’ ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Strings
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Strings
Usage-Motivation
In many applications, we need to store human-readable data, like words (name, last
name, address. . . ), sentences, or lists of alphabetical data, like the nucleic acid
sequences of DNA, . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Strings
Example
It can be :
text — "Introduction to Algorithms is a very interesting book"
Char — "C"
Empty string — ""
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Declaration of String
Syntax
name-of-string : String
Example
name : String ;
address : String ;
label : String ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Operations on String
Assignment
Str1 ← "Designing Algorithm" ;
Str2 ← "Is Amazing" ; " ;
Length() of a string returns the number of its chars
Length(Str1) = 19
Concatenation : + Str3 ← Str1 + " " + Str2 ; /* "Designing Algorithm Is Amazing"
Comparison
Str1 = Str2 , Str1 ̸= Str
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Strings in C
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Definition
Record/Structure/Compound Structure/
A record, a static data structure, is a collection of one or more
objects which can belong to different data Types.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Example of Record
Usage
Are used to gather many objects that describe a unique unit
(Client, Product, Person, Node. . . )
Declaration Syntax
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
name-record . name-of-field
Example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Array vs Record
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Declaration of Record in C
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
File
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .