Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structure Array: Lecture # 2 University of Sialkot

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

DATA STRUCTURE

ARRAY
Lecture # 2
University of Sialkot
2
Data Type
3

 Data type determines the values that can be used with the corresponding
type of data, the type of operations that can be performed on the
corresponding type of data. There are two data types:
 Built-In Data Type
 Derived Data Type
 Built-In Data Type
 Those data types for which a language has built-in support are known as built-
in data types. Most of the languages provide the following built-in data types.
 Examples: Integer, Float, Boolean, Character and String

thanks to www.tutorialspoint.com
Abstract Data Type (ADT)
4

 An abstract data type (ADT) is a set of objects together with a set


of operations. Abstract data types are mathematical abstractions;
nowhere in an ADT’s definition is there any mention of how the
set of operations is implemented.
 Objects such as lists, stacks and queues along with their
operations, can be viewed as ADTs, just as Integers, Reals and
Booleans are data types. Integers, Reals and Booleans have
operations associated with them, and so they are also called
ADTs.

thanks to www.tutorialspoint.com
Data Type (Continued…)
5

 Derived Data Type


 Those data types which are implementation independent, as they can be
implemented in one or the other way are known as derived data types.
There are normally built by the combination of built-in data types and
associated operations on them.
 Examples: List, Array, Stack, Queue
 Basic Operations
 The data in the data structures are processed by certain operations. These
operations include the following:
 Traversing, Searching, Insertion, Deletion, Sorting, Merging etc

thanks to www.tutorialspoint.com
Array
6

 Array is a container which can hold a fix number of items and


these items should be of same type. Following are the important
terms of array:
 Element & Index
 Array Representation

thanks to www.tutorialspoint.com
Total Number of Elements in array
7
Representation in Memory
8
Basic Operations of an Array
9

 Following are the basic operations supported by an array:


 Traverse – Print all the array elements one by one
 Insertion – Adds an element at the given index
 Deletion – Deletes an element at the given index
 Search – Searches an element using the given index or by the value
 Update – Updates an element at the given index

thanks to www.tutorialspoint.com
Traversing with Array (Examples)
10

 Total
 Finding even and odd
 Checking fail and pass
 More ?
Insertion with Array
11

 Insertion at end
 Insertion at start
 Insertion at the specified location
Insertion Operation
12

 Insert operation is to insert one or more data elements into an array.


Based on the requirement, a new element can be added at the
beginning, end, or any given index of array.

 Let LA be a Linear Array (unordered) with N elements and K is a


positive integer such that K<=N. Following is the algorithm where
ITEM is inserted into the Kth position of LA.

 Complexity…?

thanks to www.tutorialspoint.com
Insertion Operation (Algorithm)
13

1. Start
2. Set J = N
3. Set N = N + 1
4. Repeat Step 5 and 6 while J >= K
5. Set LA[ J + 1 ] = LA[ J ]
6. Set J = J – 1
7. Set LA[ K ] = ITEM
8. Stop

thanks to www.tutorialspoint.com
Insertion Example
14

int arr[5] = {4, 1, 8, 2};


 int val,loc;
 cout<<"enter value to insert"<<endl;

 cin>>val;

 cout<<"enter location"<<endl;
 cin>>loc;

 for(int i=4; i>loc; i--){

 // arr[i+1]=arr[i];
 arr[i]=arr[i-1];
 }

 arr[loc]=val;

 for(int i=0; i<=4; i++)


 cout<<"Values are "<<i<< " = "<<arr[i]<<endl;
Deletion Operation
15

 Deletion refers to removing an existing element from the array


and re-organizing all elements of an array.

 Consider LA is a linear array with N elements and K is a


positive integer such that K<=N. Following is the algorithm to
delete an element available at the Kthposition of LA.

 Complexity…?

thanks to www.tutorialspoint.com
16
Deletion Operation (Algorithm)
1. Start
2. Set J = K
3. Repeat Step 4 and 5 while J < N
4. Set LA[ J ] = LA[ J+1 ]
5. Set J = J + 1
6. Set N = N – 1
7. Stop

thanks to www.tutorialspoint.com
Example of deletion
17

 cout<<"\n\nEnter poss. of Element to Delete: ";


 cin>>pos;
 if(pos>5)
 {
 cout<<"\n\nThis value is out of range: ";
 }
 else
 {
 --pos;
 for(i=pos;i<=4;i++)
 {
 a[i]=a[i+1];
 }
 cout<<"\n\nNew Data in Array: ";
 for(i=0;i<4;i++)
 {
 cout<<a[i];
 }
 }
Search Operation
18

 You can perform a search for an array element based on its


value or its index.

 Consider LA is a linear array with N elements and K is a


positive integer such that K<=N. Following is the algorithm to
find an element with a value of ITEM using sequential search.

 Complexity…?

thanks to www.tutorialspoint.com
Search Operation (Algorithm)
19

1. Start
2. Set J = 0
3. Repeat Step 4 and 5 while J < N
4. IF LA [ J ] is equal ITEM THEN GOTO Step 6
5. Set J = J + 1
6. Print J, ITEM
7. Stop

thanks to www.tutorialspoint.com
Update Operation
20

 Update operation refers to updating an existing element from the array at a given
index.
 Consider LA is a linear array with N elements and K is a positive integer such
that K<=N. Following is the algorithm to update an element available at the
Kth position of LA.
1. Start
2. Set LA [ K – 1 ] = ITEM
3. Stop

 Complexity…?

thanks to www.tutorialspoint.com

You might also like