0% found this document useful (0 votes)
6 views53 pages

Chap5-Arrays

The document is a course material for 1st Year Computer Science students at Yahia Fares University, focusing on algorithms and data structures, specifically arrays. It covers the definition, declaration, operations, and exercises related to one-dimensional and two-dimensional arrays, as well as sorting algorithms. The content includes algorithms for initialization, displaying, searching, and sorting arrays, along with examples and C translations.

Uploaded by

mr sebaagamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views53 pages

Chap5-Arrays

The document is a course material for 1st Year Computer Science students at Yahia Fares University, focusing on algorithms and data structures, specifically arrays. It covers the definition, declaration, operations, and exercises related to one-dimensional and two-dimensional arrays, as well as sorting algorithms. The content includes algorithms for initialization, displaying, searching, and sorting arrays, along with examples and C translations.

Uploaded by

mr sebaagamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Yahia Fares University of Médéa

Faculty of Sciences
Department of Mathematics and Computer Science
Year: 1st Year Computer Science Licence Degree
(L.M.D)

Algorithms and Data structure 1

2024/ 2025

1
Chapter V
Arrays

2
Before get started

3
Problem
We want to write an algorithm that calculates the arithmetic
mean of N integers, then calculate the number of values
greater than the arithmetic mean already calculated.
What do you propose as data structure?

4
Disadvantages

• Using individual variables for each value can become


impractical
• Code Repetition

5
Solution
Use a global structure that consists of :
• Give one identifier (IDF) to all the variables.
• Each value will be referred to by this IDF

Array

6
One dimensional array

7
One-dimensional array : Definition

✓One-dimensional or single-dimensional array or simply an array


is a collection of several elements of the same type in a single
variable, and each element is identified by an index.
First Last
index
✓An array has a static structure index

Indexes 0 1 2 3 4 5 6
IDF NBR 4 10 2 56 11 8 0

▪ NBR is an array containing 7 integer elements Length of array is 7


▪ NBR[0] is the 1st element: NBR[0]=4 NBR[4] Element
▪ NBR[2] denotes the 3rd element 8
• Access to an array element is done by specifying the array’s
identifier (IDF) followed by the index value between square
brackets.

Example
• NBR[3] represents the 4th element of NBR array

9
Why do we need arrays ?

• We can use normal variables like (var1, var2, var3,


var4,.....) when we have a small number of objects, but if
we want to store a large number of instances, then it
becomes difficult to manage them with simple variables.

• To overcome this problem we use the concept of Array

10
One dimensional Array : Declaration
Syntax 1

Var IDF : array [𝑛𝑏𝑟_𝑒𝑙𝑡] of Type ; 𝑛𝑏𝑟_𝑒𝑙𝑡 𝑖𝑠 𝑡ℎ𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠

Example
Var Vector : array [10] of integer ; // array of 10 integer elements
C1, C2 : array [15] of character ; // arrays of 15 character elements
TAB : array [20] of boolean ; //array of 20 boolean elements
X, Y : array [100] of real ; //arrays of 100 real elements

Note : The length of an array must be known in advance. It may not be completely filled, but it cannot
contain more elements than the size specified in the declaration. 11
One dimensional Array : Declaration

Syntax 2
Type NewType = array [nbr_elt ] of Type;
Var IDF : NewType ;

Example
//we define a type called Table, which is an array of 10 integer elements.

Type Table = array [10] of integer ;


Var V, W : Table ; // declaration of variables V and W having Table type.

12
Operations on arrays

13
One dimensional array : Operations

1. Initialization (Read)
The following algorithm allows to initialize an array T of 10 integer elements.

Algorithm InitArray ;
Const N=10;
Var T : array [N] of integer ;
i : integer ;
Begin
For i  0 to N-1 do
Read (T[i]) ;
End. 14
One dimensional array : Operations

1. Initialization (Assign)
Algorithm InitArray ;
We can initialize T elements using Var T : array [4] of integer ;
assign operation by specifying each Begin
element index. T[0]  7; // assign 7 to 1st element
The following algorithm allows to T[1]  0; // assign 0 to 2nd element
initialize an array T of 4 integer T[2]  8; // assign 8 to 3rd element
elements. T[3]  14; // assign 14 to 4th element
End.

15
One dimensional array : Operations

2. Displaying (Write):
The following algorithm allows displaying elements of an array of 10 integers

Algorithm displayArray ;
Const N=10;
Var T : array [N] of integer ;
i : integer ;
Begin
for i  0 to N-1 do
read(T[i]) ;
for i  0 to N-1 do
write (T[i]) ; 16
End.
One dimensional array : Operations
Algorithm SearchVal ;
Const N=10;
3. Searching a value in an array Var T : array [N] of integer ;
i , x : integer ;
found : boolean;
We have an array of 10 integers. We Begin
want to determine if a user-input value For i  0 to N-1 do
x is present in this array and if so, find Read (T[i]) ;
its first position. Read (x) ;
i0 ; found  False ;
While (i<=N-1) and Not found do
begin
If T[i]=x then foundTrue ;
i i + 1 ;
end;
if found then write (″This value exists in position″ , i-1)
else write (″This value does not exist″) ;
End. 17
Translation in C

18
One dimensional array : Exercise
Let we consider T an array of N integers (0<N≤100). Write an algorithm that calculates
the sum of T elements.
Algorithm SumArray ;
Var T : array [100] of integer ;
N, i, S : integer ;
Begin
Do
Read (N);
while (N<=0 or N>100) ;
For i  0 to N-1 do
Read (T[i]) ;
S0;
For i  0 to N-1 do
S  S + T[i] ;
Write (S);
End. 19
Algorithm MinMaxArray ;
Var T : array [100] of integer ;
One dimensional array : Exercise i, N, min, max, x : integer ;
// min is the minimum element index
// max is the minimum element index
Let T be an array of N integer values (0<N≤100). Begin
Do
Write an algorithm that determines the maximum Write("Give N>0 and <=100");
and the minimum of T, then swaps them. Read (N) ;
while (N>100) or (N<0) ;
For i  0 to N-1 do Read (T[i]) ;
min  0 ; max  0 ;
For i  1 to N-1 do
if T[i] > T[max] then max  i ;
else if T[i] < T[min] then min  i;
Write(T[max],T[min]);
x  T[max] ;
T[max]  T[min] ;
T[min]  x;
For i de 0 to N-1 do
Write (T[i]) ;
20
End.
Translation in C

21
Two-dimensional Array

22
Two dimensional array : Definition
A two-dimensional array, also known as a 2D array or Matrix , is an array
whose elements are arranged in a grid-like structure with rows and
columns.
Each element in the array is referred to as a cell and can be accessed by
its row and column indexes.

23
Two dimensional array : Declaration
Syntax 1
𝑛𝑏𝑟_𝑟𝑤 𝑖𝑠 𝑡ℎ𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑟𝑜𝑤𝑠
Var IDF : array [𝑛𝑏𝑟_𝑟𝑤 ] [ 𝑛𝑏𝑟_𝑐𝑙] of Type ;
𝑛𝑏𝑟_𝑐𝑙 𝑖𝑠 𝑡ℎ𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑐𝑜𝑙𝑢𝑚𝑛𝑠

Example

Var Mat : array [10][20] of integer;


A , B : array [20][ 3] of real;

24
Two dimensional array : Declaration
Syntax 2
Type NewType = Array [ nbr_rw ] [nbr_cl ] of Type;
Var IDF : NewType ; Graphic Representation

Example Mat[1][2]

Type Tab = array [2][3] of integer ;


Var Mat : Tab ; Mat[0][0]

Mat[i][j] : represents the element of Mat matrix with the indexes : row i and column j
25
Two dimensional array : operations
1. Initialization (Read)
The following algorithm initializes an
integer matrix of dimensions 2x3.

Algorithm InitMatrix ;
Const N=2 ; M=3;
Var Mat : array [N][M] of integer ;
i, j : integer ;
Begin
For i  0 to N-1 do
For j  0 to M-1 do
Read(Mat[i][j]) ;
End.
26
Translation in C

27
Two dimensional array : operations
2. Displaying (write)
The following algorithm allows you to input and display elements of an integer 2D array (2x3).

Algorithm MatrixDisplay ;
Const N = 2; M=3;
Var Mat : Array [N][M] of integer ;
i , j : integer ;
Begin
For i  0 to N-1 do
For j  0 to M-1 do
Read (Mat[i][j]) ;
For i 0 to N-1 do
For j  0 to M-1 do
Write (Mat[i][j]) ;
End. 28
Algorithm MatrixResearch ;
Const N = 5; M=10;
Var Mat : array[N][M] of integer ;
Two dimensional array : operations i, j, X : integer;
found: boolean ;
Begin
3. Searching a value
For i  0 to N-1 do
Let we consider a 2D array of 5x10 integers; we are For j  0 to M-1 do
Read(Mat[i][j]) ;
looking to determine whether a user-input value X
Read (X) ; found  false ; i0 ;
belongs to this matrix and display the position of while (i<=N-1) and Not found do
the first occurrence. begin
j0;
while (j<=M-1) and Not found do
begin
if mat[i][j]=X then foundtrue;
j j + 1 ;
end ;
i i + 1 ;
end ;
if found then write (″this value exists in the
position ″, i-1, j-1);
else write(″this value does not 29exist″) ;
end.
Two dimensional array: Exercise 1
We consider a matrix of NN integers (N<=10). Write an algorithm that:
1. Reads the matrix T.
2. Calculate the trace of T (Trace= Sum of elements on the main diagonal)
Algorithm Trace ;
Var T : Array [10][10] of integer ;
i, j, N, S : integer ;
Begin
Do
Read (N);
while (N>10) or (N<0);
For i  0 to N-1 do
For j  0 to N-1 do
Read(T[i][j]) ;
S0;
For i  0 to N-1 do
S  S + T[i][i] ;
write (S);
End.
30
Translation in C

31
Two dimensional array: Exercise 2
Let we consider a matrix T of NN integers (N<=10). Write an algorithm that reads T elements and swaps the 1st
row with the 1st column.

Algorithm Swapversion ;
Var T : Array[10][10] of integer ;
i, j, N, X : integer;
Begin
Do
Read (N);
While(N>10) or (N<0);
For i  0 to N-1 do
For j  0 to N-1 do
Read(T[i][j]) ;
For j  1 to N-1 do
begin
X  T[0][j] ;
T[0][j]  T[j][0] ;
T[j][0]  X ;
end;
For i  0 to N-1 do
For j  0 to N-1 do
write(T[i][j]) ;
end. 32
Translation in C

33
Sorting

34
Sorting algorithms

A sorting algorithm is an algorithm made up of a series of instructions that


takes an array as input, performs specified operations to rearrange it
according to a comparison operator on the elements.
Unsorted array

25 35 18 3 27 12 47 8 10 30

Sorted array in ascending order

3 8 10 12 18 25 27 30 35 47

Sorted array in descending order

47 35 30 27 25 18 12 10 8 3
35
Sorting algorithms

There are many sorting methods, some of which are more or less easy to
implement, and some are significantly more efficient than others.
We can cite :
✓ Insertion sort
✓ Bubble sort
✓ Selection sort
✓ Counting Sort
✓ Merge sort
✓ Quick sort
… Etc. 36
Sorting algorithms
1. Selection sort
Selection sort iterates through the array and finds
the smallest number in the array and swaps it
with the first element if it is smaller than the first
element.
Next, it goes on to the second element and so on
until all elements are sorted.

✓ Simple to implement
 Inefficient for large arrays

37
Selection sort visualization

38
Algorithm Selection_sort ;
Const N = 100 ;
Var Tab : Array [N] of integer ;
i, j, pmin, x : integer ;
Begin
For i0 To N-1 Do
read(Tab[i]) ;
For i0 To N-2 Do
begin
pmini ;
For j i+1 To N-1 Do
if Tab[j]<Tab[pmin] then pminj ;
if i<>pmin then begin
xTab[i] ;
Tab[i]Tab[pmin];
Tab[pmin]x;
end;
end ;
For i 0 To N-1 Do
write (Tab[i]) ;
End. 39
Sorting algorithms
2. Bubble sort
Bubble Sort is based on the idea of repeatedly comparing
pairs of adjacent elements and then swapping their positions
if they exist in the wrong order.

✓ Easy to understand
 Very inefficient for large arrays

40
Bubble sort visualization

41
Algorithm bubble_sort;
Const N = 100 ;
Var Tab : Array [N] of integer ;
i, j, x : integer ;
Begin

For i0 To N-1 Do


read(Tab[i]) ;

For i  0 To N-2 Do
For j  0 To N-i-2 Do
if Tab[j] > Tab[j+1] Then begin
x  Tab[j];
Tab[j]  Tab[j + 1];
Tab[j + 1]  x;
end;
For i 0 To N-1 Do
write (Tab[i]) ;
End.
42
Sorting algorithms
3. Insertion sort
Insertion sort starts iteration from the second element
of the array and compares each element with the ones
before it.
If the element being compared is smaller than the one
before it, then the algorithm swaps the two elements
(and keeps swapping with the next element before it
after the last swap) until the smaller element is in its
correct sorted position.

✓ Efficient for small arrays


 Less efficient for large arrays
43
Insertion sort visualization

44
Algorithm insertion_sort;
Const N = 100 ;
Var Tab : Array [N] of integer ;
i, j, x : integer ;
Begin
For i0 To N-1 Do
read(Tab[i]) ;
For i  1 To N-1 do
begin
x Tab[i];
j  i - 1;
while (j >= 0 and x < Tab[j] ) Do
begin
Tab[j + 1]  Tab[j];
j  j - 1;
end;
Tab[j + 1]  x;
end;
For i 0 To N-1 Do
write (Tab[i]) ;
End. 45
Exercises with solutions

46
Exercises
Exercise 1
Let T be an array containing N integers (N ≤ 100). Write an algorithm
that finds the first element that appears the most times in T, as well as
its number of occurrences.
Note : don’t use another array
• Example:

The algorithm displays : ‘The value 5 appears 4 times’

47
Exercise 1 : solution
Algorithm NB_Max ;
Var T : Array [100] of integer ;
N, i, j, cnt, max, pos :integer ;
Begin
Do
read (N);
while( N>100) or (N<0);
For i  0 to N-1 do
read(T[i]) ;
max0;
For i  0 To N-2 do
Begin
cnt 1;
For j  i+1 to N-1 do
if T[i]=T[j] then cnt cnt + 1;
if cnt>max then begin
max  cnt;
pos  i ;
end;
end;
write (″The value ″, T[pos] , ″ appears ″, max, ″ times″) ;
End. 48
Exercises
Exercise 2

Let there be a square matrix A (nn) of integer numbers (0<n≤50).


Write an algorithm that counts, for each row i, the number of elements (NB) whose
value is strictly greater than the value of the main diagonal element, and put this
number in an array T(n).

49
Exercise 2 : solution
Algorithm GreaterElements;
Var A: array[50][50] of integer;
T: array[50] of integer;
N, i, j, cnt: integer;
Begin
Do
read(N);
while N<=0 or N>50;
For i  0 To N - 1 do
For j  0 To N - 1 do
read(A[i][j]);
For i  0 To N - 1 do
begin
cnt  0;
For j  0 To N - 1 do
if A[i][j] > A[i][i] then cntcnt + 1;
T[i]cnt;
end;
For i 0 To N - 1 do
write(T[i]); 50
end.
Exercise 2 : solution

51
Exercises
Exercise 3
Let T be an integer array of size 100 containing N integer values
(N<100) sorted in ascending order. Write an algorithm that inserts an
integer entered value V into T by keeping the ascending order without
using another array.
Example

52
Exercises
Exercise 4

Write an algorithm that merges two arrays T1(m) and T2(n) sorted in
ascending order, into a single table T3(m+n) sorted in ascending order.
Note : 0<m≤100, 0<n≤100

Example :

53

You might also like