Data Structure
Data Structure:
Data Structure is a specialized format for organizing and storing the data.
Classification of Data Structure:
1. Primitive Data Structure.
2. Non-Primitive Data Structure.
Primitive Data Structure:
Primitive Data Structures are directly operated upon machine level instruction are
known as primitive data structures.
Examples: int, float (real) , logical data , Character , pointers and references.
Operations on primitive Data Structure:
The various operations can be operated on primitive data structure are:
1. Create: Create operation is used to create a new data structure. This data
structure reserves memory to store the program element.
Example: int x;
2. Destroy: Destroy operation is used to destroy or remove the data structure
from the memory.
When the program execution ends, the data structure is automatically
destroyed.
3. Select: Select operation is used to by programmers to access the data within
the data structure. This operation updates or alter the data.
4. Update: Update operation is used to change the data of data structure.
Example: int x=5; // Here 5 is assigned to x
Again x = 4; // ‘4’ is reassigned to x. The value of x is
4 now because 2 is automatically replaced by 4, i.e. updated
Non-primitive data structure:
Non-primitive data structures are not directly manipulated by the machine level
instruction. These are derived from primitive data structure.
Non-primitive data structures are classified as Arrays, lists and files.
Data structure under list are classified as Linear and non-linear data structure.
Linear data structure:
MICA, Mysuru Ms. Manasa, Dept of CS Page |1
Linear data structures are data structure where elements are organized one after the
other in sequential manner.
Example:Arrays, Stacks, queues and linked list.
Non-linear data structure:
A non-linear data structure is a data structure in which one data item is connected
to several other data items.
Examples: trees and graph.
Operations on linear data structure/non-primitive/non-linear:
1. Traversal: The process of accessing each data item exactly at once to
perform some operation.
2. Insertion: The process of adding a new item into the given collection of data
items is called insertion.
3. Deletion: The process of removing an existing data item from the given
collection of data items is called deletion.
4. Searching: The process of finding the location of a data item in the given
collection of data items is called as searching.
5. Sorting: The process of arrangement of data items in ascending or
descending is called as sorting.
6. Merging : The process of combining two data structure into single data
strcture.
****************************************************
MICA, Mysuru Ms. Manasa, Dept of CS Page |2
Arrays
Arrays is a collection of homogeneous elements with same name and same type.
The elements are organized one after the another in adjacent memory location.
Types of an arrays:
1. One-dimensional Array
2. Two-dimensional Array
3. Multi-dimensional Array
One-dimensional Array:
An array with only one row or column is called one-dimensional array.
Or (An array with one subscript is called1-d array)
Array can be denoted as: data type Arrayname[size];
int a[10];
Here, size specifies the number of elements in the array and index value starts
from 0 to n-1.
Calculating the length of Array:
The length of an array can be calculated by,
L = UB – LB+1;
Where, UB is the last index value
LB is the first index value
Example: If an array A has values 15,25,35,45,55 stored in location 0,1,2,3,4 then
UB=4 and LB=0
L=4-0+1
L=5
Representation of one-dimensional array:
Array with one subscript is known as one-dimensional array. elements are stored in
continuous memory location. Address of the first element of an array is called Base
address. Using Base address , we can calculate of any elemet of an
MICA, Mysuru Ms. Manasa, Dept of CS Page |3
Loc(A[P])=Base(A)+w*(P-LB)
Where , P is the position of the element
Base(A) is the address of the first element
W is the words per memory cell
Example: Suppose if ‘A’ is an array used to store integer value 10,20,30,40,50
respectively. Address of the first element is 1000 and 1 byte per memory cell then
find the address o fourth element.
1000 1001 1002 1003 1004
10 20 30 40 50
A[0] A[1] A[2] A[3] A[4]
Given: Base(A)=1000 , P=3, W=1,LB=0
A[P]= Base(A)+W*(P-LB)
A[3]=1000+1*(3-0)
A[3]=1000+3
A[3]=1003
Basic operation performed on Arrays:
1. Traversal
2. Insertion
3. Deletion
4. Searching
5. Sorting
6. Merging
Traversal
• Traversing is the process of visiting each subscript at least once from the
beginning element to last element.
• For example, to find the maximum element of the array we need
to access each element of the array.
• ALGORITHM: Traversal (A, LB, UB) A is an array with Lower
Bound LB and Upper Bound UB.
MICA, Mysuru Ms. Manasa, Dept of CS Page |4
Step 1: for i LB to UB do
PROCESS A [i];
[End of for loop]
Step 2: Exit
Insertion:[inserting an element into an array]
The process of inserting an element into an array is called insertion.
ALGORITHM: Insert (A, N, ITEM, Pos) A is an array with N
elements. ITEM is the element to be inserted in the position Pos.
Step 1: for I =n-1 to pos , do
A[I+1] =A[I];
End for
Step 2: A[pos] = item;
Step 3: N=N+1;
Step 4: Exit
Deletion: [deleting an element into an array]
Deletion is the process of removing an element from data structure.
ALGORITHM: Insert (A, N, ITEM, Pos) A is an array with N elements.
ITEM is the element to be inserted in the position Pos.
Step 1: item =A[Pos];
Step 2: for I=Pos to n-1
A[I]=A[I+1];
End for
Step 3: N=N-1;
Step 4: Exit
MICA, Mysuru Ms. Manasa, Dept of CS Page |5
Searching :
Searching is the process of finding the location of the given element in a
data structure.
There are two types of searching :
1. Linear search
2. Non-linear search
Liner searching: This is the simplest method in which the elements to
be compared with each element of the array one by one from the
beginning to till end of the array.
Algorithm :
‘A’ is an array with N elements. ‘ELE’ is an element to be searched. This
Algorithm finds the location ‘loc’ where the given element is stored.
Step 1: loc=-1;
Step 2: for i=0 to N-1
If (a[i]==ele)
Loc=i;
Goto step3;
End if
End for
Step 3: if (loc>=0)
Print Loc;
Else
Print “Searching is unsuccessful.”
Binary Search :
When the elements of the array are in sorted order , the best method for searching
is binary search. This method compares the searching element with the middle
MICA, Mysuru Ms. Manasa, Dept of CS Page |6
element of the array . if the comparison does not match , element is searched either
at the right-half or left-half of the array.
Algorithm :
‘A’ is the sorted array with LB as lower bound and UB as upper bound
respectively. Let low , high , mid denote beginning, end and middle index value of
the array A.
Step 1: set low=0;
high=n-1;
loc=-1;
step 2: while ( low < = high )
mid = low +high/2;
if (ele==mid)
loc = mid ;
goto step 3;
else if (ele < a[mid])
low = mid -1;
else
high = mid+1;
end if-else
end for
Step 3: if (loc >=0)
Print loc ;
Else
Print “ searching is unsuccessful”
Step 4: exit
MICA, Mysuru Ms. Manasa, Dept of CS Page |7
Sorting the elements:
Sorting is the arrangement of elements in ascending or descending order. There are
various method like bubble sort , insertion sort , quock sort etc.
Insertion sort:
The first element of the array is assumed to be in correct position. The next
element is considered as key element and compared with the previous element and
is inserted in its correct position.
Algorithm:
Let A be an array with N elements.
Step 1: for I=1 to n-1;
J=I;
Step 2: While (J>=1)
If ( a[ J ] < a [ J -1 ])
Temp= a [ J ];
a[ J ]=a[ J-1];
a[ J -1 ]=Temp;
end if;
J--;
end for
end while;
step 3: exit;
Two-dimensional Array:
An array with two subscript is called two-dimensional array.
• The elements of two-dimensional array as rows and columns.
• The number of rows and columns in a matrix is called as the order of
the matrix and denoted as m x n.
MICA, Mysuru Ms. Manasa, Dept of CS Page |8
• The number of elements can be obtained by multiplying number of
rows and number of columns.
0 1 2
A[0] 11 12 13
A[1] 21 22 23
A[2] 31 32 33
Representation of Two-Dimensional Array:
•A is the array of order m x n. To store m*n number of elements, we
need m*n memory locations.
The elements should be in contiguous memory locations.
• There are two methods:
1. Row-major method
2. Column-major method
• Row-Major Method: All the first-row elements are stored in
sequential memory locations and then all the second-row elements are
stored and so on.
1000 11 A [0][0] 1000 11 A [0][0]
1001 12 A [0][1] 1001 21 A [1][0]
1002 13 A [0][2] 1002 31 A [2][0]
A [1][0]
1003 21 1003 12 A [0][1]
A [1][1]
1004 22 A [1][2] 1004 22 A [1][1]
1005 23 A [2][0] 1005 32 A [2][1]
1006 31 A [2][1] 1006 13 A [0][2]
1007 32 A [2][2] 1007 23 A [1][2]
1008 33 1008 33 A [2][2]
Row major order Column major order
Formula to find the address of 2-dimension array using row-major
and column major order:
MICA, Mysuru Ms. Manasa, Dept of CS Page |9
Row-Major Order:
A[i][j]=Base(A)+[(i-LB)*n+(j-LB)]*W;
i and j are row and column number respectively.
LB is the index of first element
m and n are number of row and columns.
W is the number of bytes per memory cell.
Column- major order:
A[i][j]=Base(A)+[(i-LB)+(j-LB)*m]*W;
Advantages of Array:
• It is used to represent multiple data items of same type by using
single name.
• It can be used to implement other data structures like linked lists,
stacks, queues, tree, graphs etc.
• Two-dimensional arrays are used to represent matrices.
• Many databases include one-dimensional arrays whose elements are
records.
Disadvantages of Array:
• We must know in advance the how many elements are to be stored in
array.
• Array is static structure. It means that array is of fixed size. The
memory which is allocated to array cannot be increased or decreased.
• Array is fixed size; if we allocate more memory than requirement then
the memory space will be wasted.
• The elements of array are stored in consecutive memory locations. So
insertion and deletion are very difficult and time consuming.
MICA, Mysuru Ms. Manasa, Dept of CS P a g e | 10