Data Structure Array: Lecture # 2 University of Sialkot
Data Structure Array: Lecture # 2 University of Sialkot
Data Structure Array: Lecture # 2 University of Sialkot
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
thanks to www.tutorialspoint.com
Data Type (Continued…)
5
thanks to www.tutorialspoint.com
Array
6
thanks to www.tutorialspoint.com
Total Number of Elements in array
7
Representation in Memory
8
Basic Operations of an Array
9
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
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
cin>>val;
cout<<"enter location"<<endl;
cin>>loc;
// arr[i+1]=arr[i];
arr[i]=arr[i-1];
}
arr[loc]=val;
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
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