0% found this document useful (0 votes)
14 views

Data Structure & Algorithms: M. Arslan Idris - CS-214

The document discusses arrays and their basic operations. It defines an array as a container that can hold a fixed number of items of the same type. Arrays allow storing multiple values in a single variable and accessing the values using indexes. The document covers array declaration, initialization, representation, and basic operations like traversal, insertion, deletion, and search. Algorithms to perform each operation are provided.

Uploaded by

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

Data Structure & Algorithms: M. Arslan Idris - CS-214

The document discusses arrays and their basic operations. It defines an array as a container that can hold a fixed number of items of the same type. Arrays allow storing multiple values in a single variable and accessing the values using indexes. The document covers array declaration, initialization, representation, and basic operations like traversal, insertion, deletion, and search. Algorithms to perform each operation are provided.

Uploaded by

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

Data Structure &

Algorithms
M. Arslan Idris| CS-214

Data Structure & Algorithms(Fall 2020)


Background-Why array
Let’s say you want to declare four integers. You would do something like this:

int a;
int b;
int c;
int a,b,c,d;

What if you wanted to declare a thousand variables?

Data Structure & Algorithms(Fall 2020)


Cont..

Imagine that we have 100 scores. We need to read them, process them and print them.
We must also keep these 100 scores in memory for the duration of the program. We
can define a hundred variables, each with a different name, as shown in Figure.

Figure A hundred individual variables

Data Structure & Algorithms(Fall 2020)


An array is a sequenced collection of elements, normally of the same data type, We
can refer to the elements in the array as the first element, the second element and
so forth, until we get to the last element.

Figure 11.3 Arrays with indexes

Data Structure & Algorithms(Fall 2020)


Array - Background

same nature !
An array of airplanes

An array of bugs

An array of cards

Data Structure & Algorithms(Fall 2020)


Arrays

 Array is a container which can hold fix number of items and these items should be
of same type

 Most of the data structure make use of array to implement their algorithms.
Following are important terms to understand the concepts of Array.

 Element − Each item stored in an array is called an element.

 Index − Each location of an element in an array has a numerical index which is used to
identify the element.

Data Structure & Algorithms(Fall 2020)


Cont.. Arrays

 An array is defined as a set of finite number of


homogeneous elements or same data items.

 It means an array can contain one type of data only, either


all integer, all float-point number or all character.

Data Structure & Algorithms(Fall 2020)


Array
An array lets you declare and work with a collection of values of
the same type. If you want to declare 5 variable ?

int a[5];

int a[1000];

Data Structure & Algorithms(Fall 2020)


Array Representation
 Arrays can be declared in various ways in different languages.
For illustration, let's take C++ array declaration.

Data Structure & Algorithms(Fall 2020)


Cont.. Array Representation

 Following are the important points to be considered.

 Index starts with 0.

 Array length is 10 which means it can store 10 elements.

 Each element can be accessed via its index. For example, we can fetch element at index
6 as 9.

Data Structure & Algorithms(Fall 2020)


Arrays

 Following are some of the concepts to be remembered about


arrays:

 The individual element of an array can be accessed by specifying name of


the array, following by index or subscript inside square brackets.

 The first element of the array has index zero[0]. It means the first element
and last element will be specified as: arr[0] & arr[9] Respectively.

Data Structure & Algorithms(Fall 2020)


Arrays

 The elements of array will always be stored in the


consecutive (continues) memory location.

 The number of elements that can be stored in an array,


that is the size of array or its length is given by the
following equation:
(Upperbound-lowerbound)+1

Data Structure & Algorithms(Fall 2020)


Arrays

 For the above array it would be


(9-0)+1=10,where 0 is the lower bound of array
and 9 is the upper bound of array.

 Array can always be read or written through loop. If


we read a one-dimensional array it require one loop
for reading and other for writing the array.

Data Structure & Algorithms(Fall 2020)


Initializing Arrays

• By default, regular arrays are left uninitialized.


• This means that none of its elements are set to any particular
value.
• Their contents are undetermined at the point the array is
declared
• But the elements in an array can be explicitly initialized to
specific values when it is declared, by enclosing those initial
values in braces {}.

Data Structure & Algorithms(Fall 2020)


Initializing Arrays
• For example:

int foo [5] = { 16, 2, 77, 40, 12071 };

This statement declares an array that can be represented like


this:

Data Structure & Algorithms(Fall 2020)


Initializing Arrays

int bar [5] = { 10, 20, 30 }; ?


If declared with less, the remaining elements are set to their
default values (which for fundamental types, means they are
filled with zeroes).

Data Structure & Algorithms(Fall 2020)


Initializing Arrays

?
 
int baz [5] = { };

This creates an array of five int values, each initialized with a


value of zero:

Data Structure & Algorithms(Fall 2020)


Initializing Arrays

int foo [] = { 16, 2, 77, 40, 12071 }; ?


In this case, the compiler will assume automatically a size for
the array that matches the number of values included between
the braces {}:

Data Structure & Algorithms(Fall 2020)


Basic operations on Arrays

 Following are the basic operations supported by an array.


 Traverse − print all the array elements one by one.

 Insertion − add an element at given index.

 Deletion − delete an element at given index.

 Search − search an element using given index or by value.

 Update − update an element at given index.

Data Structure & Algorithms(Fall 2020)


Insertion Operation
 Insert operation is to insert one or more data elements into an
array.
 Algorithm: Let Array is a linear unordered array of MAX elements.
 Example
 Result: Let LA is a Linear Array unordered with N elements and K is a positive integer
such that K<N. Below is the algorithm where ITEM is inserted into the Kth position of
LA.

Data Structure & Algorithms(Fall 2020)


Data Structure & Algorithms(09-02-2019)
Deletion Operation
 Deletion refers to removing an existing element from the array
and re-organizing all elements of an array.
 Algorithm: Consider LA is a linear array with N elements and K is a positive
integer. Below is the algorithm to delete an element available at the Kth position of
LA.

Data Structure & Algorithms(Fall 2020)


Data Structure & Algorithms(09-02-2019)
Search Operation
 You can perform a search for array element based on its value
or its index.
 Algorithm: Consider LA is a linear array with N elements and K is a positive
integer such that K<=N. Below is the algorithm to find an element with a value of
ITEM using sequential search.

Data Structure & Algorithms(Fall 2020)


Data Structure & Algorithms(09-02-2019)
Data Structure & Algorithms(Fall 2020)
Data Structure & Algorithms(Fall 2020)

You might also like