0% found this document useful (0 votes)
12 views10 pages

II PUC CS Datastucture PDF

Chapter 4 discusses data structures, defining them as specialized formats for organizing and storing data. It classifies data structures into primitive (e.g., int, float) and non-primitive (e.g., arrays, lists) types, and details operations performed on them such as creation, deletion, and searching. The chapter also covers arrays, including their advantages, disadvantages, types, and basic operations like insertion, deletion, and sorting.

Uploaded by

ruabkje503
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)
12 views10 pages

II PUC CS Datastucture PDF

Chapter 4 discusses data structures, defining them as specialized formats for organizing and storing data. It classifies data structures into primitive (e.g., int, float) and non-primitive (e.g., arrays, lists) types, and details operations performed on them such as creation, deletion, and searching. The chapter also covers arrays, including their advantages, disadvantages, types, and basic operations like insertion, deletion, and sorting.

Uploaded by

ruabkje503
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/ 10

Chapter 4

DATA STRUCUTRE
Data Structure: It is a specialized format for organizing and storing of data.

[Data: Data is a collection of facts, figures, statistics which can process to produce meaningful
information.
Information: Information is processed data with definite meaning.]

Classification of Data Structures

Primitive data structure: The data structures which are directly operated on machine level
instructions.
Ex: int, float, pointers etc…
Non-primitive data structure: The data structures which are derived from primitive data
structures are called non-primitive operating system.
Ex: arrays, lists, files etc…

Operations performed on primitive data structures

(1) Create: This operation is used to create new data structure.


(2) Destroy: This operation is used to remove unwanted data structures.
(3) Select: This operation is used to extract required data from data structure
(4) Update: This operation is used to modify the contents of a data structure

Linear data structure: The collection of homogeneous elements arranged in a linear order
Ex: stacks, queues, linked list etc…

Operations performed on linear data structure

1) Traversing: This operation is used to visit or access each element of the list at least once
2) Inserting: This operation is used to add a new element into the list.
3) Deleting: This operation is used to remove an unwanted element from the list
4) Searching: This operation is used to find the location of the element in the list
5) Sorting: This operation is used to arrange the elements in ascending or descending order
6) Merging: This operation is to combining the data items of two lists into a single list.

Arrays
It is a collection of homogeneous elements all are of same data type under the same
name.

Advantages or applications of Arrays


1. The number of similar type of elements can be stored under single name.
2. Two-dimensional array is used to implement matrix applications
3. Arrays are used to implement other data structures like stacks and queues.

Disadvantages of Arrays
1. The array size must be pre-known
2. Only similar type of elements can be stored.
3. These may be a chance of shortage and wastage of memory.

Types of Arrays
1) One dimensional array
2) Two-dimensional array
3) Multi-dimensional array
1) One dimensional array:

Declaration:
datatype array name[size];

ex :int a[10];

Initialization of one dimensional array:

datatype array name[size] = { elements};

Ex: int a[5] = { 10, 20, 30, 40, 50};

int a[0] = 10
int a[1] = 20
int a[2] = 30
int a[3] = 40
int a[4] = 50

Basic operations on one-dimensional arrays


1. Traversing: Accessing each element of the array exactly once to do some operation.
2. Searching: Finding the location of an element in the array.
3. Sorting: Arranging the elements of the array in some order.
4. Insertion: Inserting an element into the array.
5. Deletion: Removing an element from the array.
6. Merging: Combining one or more arrays to form a single array.

(i). Algorithm for Traversing an array


Let A be a linear array. This algorithm traverses each element of array A.
Step 1: start
Step 2: for (i = 0 to n-1)
Step 3: input a[i]
End of for loop
Step 4: Exit
2. Searching
 It refers to finding the location of the element in an array.
Types of searching:
i. Linear Search
ii. Binary Search

a) Linear search :
In this method searching can be done sequentially one after the other.
This is the simplest method in which the element to be searched is compared with each element
of the array one by one from the beginning till end of the array.
Since searching is one after the other it is also called as sequential search or linear search.

(ii). Algorithm for Linear Search:


A is the name of the array with N elements. ele is the element to be searched. This
algorithm finds the location LOC where the search ele element is stored.
Step 1 : LOC = -1
Step 2 : for (i = 0 to N-1)
step 3 : if( ele == A[i])
step 4 : LOC = i
GOTO 5
End of if
End of for
Step 5 : if(LOC >= 0)
PRINT “Element found at location”, LOC+1
else
PRINT “Search is unsuccessful”
Step 6 : Exit

b) Binary search:
When the elements of the array are in sorted order, the best method of searching is
binary search.
In this method searching can be done by dividing it into 2 groups.
This method compares the element to be searched with the middle element of the array. If the
comparison does not match the element is searched either at the right-half of the array or at the
left-half of the array.
Let B and E denote the beginning and end locations of the array. The middle element
A[M] can be obtained by first finding the middle location M by M = int(B+E)/2, where int is the
integer value of the expression.
If A[M] = ELE, then search is successful. Otherwise a new segment is found as follows:
1. If ELE<A[M] , searching is continued at the left-half of the segment. Reset E = M – 1.
2. If ELE>A[M], searching is continued at the right-half of the segment. Reset B = M + 1.
3. If ELE not found then we get a condition B>E. This results in unsuccessful search.

(iii). Algorithm for Binary Search:


A is the sorted array with. Let B, E, M denote beginning, end and middle locations of the
segments of A.
Step 1 : set B = 0, E = N-1, LOC=-1
Step 2: while (B <= E)
step 3 : M= int(B+E)/2
step 4 : if(ELE == A[M])
step 5 : LOC = M
GOTO 7
else
step 6 : if(ELE <A[M])
E = M-1
else
B = M+1
End of while
Step 7: if(LOC >= 0)
PRINT “Element found at location”, LOC+1
else
PRINT “Search is unsuccessful”
Step 8: Exit

3. Insertion an element
Insertion refers to inserting an element into the array. A new element can be done
provided the array should be large enough to accommodate the new element.
When an element is to be inserted into a particular position, all the elements from the
asked position to the last element should be shifted into the higher order positions.

Example: Let A be an array with items 10, 20, 40, 50 and 60 stored at consecutive locations.
Suppose item=30 has to be inserted at position 2. The following procedure is applied.
Move number 60 to position 5.
Move number 50 to position 4.
Move number 40 to position 3
Position 2 is blank. Insert 30 into the position 2. i.e., A[2] = 30.
(iv). Algorithm for insertion:
A is the array with N elements. ele is the element to be inserted in the position P.
Step 1: for( i = N-1 down to P)
step 2: A[i+1] = A[i]
End of for
Step 3: A[P] = ele
Step 4: N = N+1
Step 5: Exit

4. Deleting an element from the array

Deletion refers to removing an element from the array. When an element is to be


deleted from a particular position, all the subsequent shifted into the lower order positions.

Example: Let A be an array with items 10, 20, 30, 40 and 50 stored at consecutive
locations. Suppose item=30 has to be deleted at position 2. The following procedure is applied.
Copy 30 to Item. i.e., Item = 30
Move number 40 to position 2.
Move number 50 to position 3.

(v). Algorithm for Deletion:


A is the array with N elements. ele is the element to be deleted in the position P and it is
stored into the variable ele.
Step 1: ele = A[P]
Step 2: for i = P to N-2
step 3: A[i] = A[i+1]
End of for
Step 4: N = N-1
Step 5: Exit
5. Sorting the elements
Sorting is the arrangement of elements of the array in ascending or descending
order.
There are various methods of sorting like bubble sort, shell sort, selection sort, quick sort,
heap sort, insertion sort etc.

Insertion Sort:
The first element of the array is assumed to be in the correct position. The next element is
considered as the key element and compared with the elements before the key element and is
inserted in its correct position.
Example:
Consider the following list of numbers 70 30 40 10 80 stored in the consecutive
locations.
Step 1: Assuming 70 in correct position. 70 is compared with 30. Since 30 is less than 70
the list now is 30 70 40 10 80
Step 2: Now 40 is the key element. First it is compared with 70. Since 40 is less
than 70 it is inserted before 70. The list now is 30 40 70 10 80
Step 3: Now 10 is the key element. First it is compared with 70. Since it is less it is exchanged.
Next it is compared with 40 and it is exchanged. Finally it is compared with 30 and placed
in the first position. The list now is 10 30 40 70 80
Step 4: Now 80 is the key element and compared with the sorted elements and placed in the
correct position. Since 80 is greater than 70 it retains its position.

(vi). Algorithm of insertion sort:


Let A be an array with N unsorted elements. The following algorithm sorts the elements
in order.
Step 1: for i = 1 to N-1
Step 2: J = i
step 3: While ( J >= 1 )
step 4: if( A[J] < A[J-1])
temp = A[J]
A[J] = A[J-1]
A[J-1] = temp
if end
step 5: J = J-1
While end
for end
Step 6: Exit
Two-dimensional arrays
A two dimensional array is a collection of elements and each element is identified by a
pair of indices called as subscripts. The elements are stored in contiguous memory locations.

In this matrix, the total number of elements in the array would


be, rows x columns = 3 x 3 =9-elements.

Memory Representation of 2-dimensional array


Suppose 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 order
2. Column-major order

Row-major order
Let A be the array of order m x n. In row-major order, all the first-row elements are
stored in sequential memory locations and then all the second-row elements are stored and so on.

Base(A) is the address of the first element. The memory address of any element A[I][J]
can be obtained by the formula
LOC(A[I][J]) = Base(A) + W[n(I-LB) + (J-LB)]
where W is the number of words per memory location. LB=lower bound,
UB=upper bound
Example: Consider the array of order 3 x 3.

Column-major order
Let A be the array of order m x n. In column-major order, all the first column elements
are stored in sequential memory locations and then all the second-column elements are stored
and so on.
Base(A) is the address of the first element. The memory address of any element A[I][J]
can be obtained by the formula
LOC(A[I][J]) = Base(A) + W[(I-LB) + m(J-LB)]
where W is the number of words per memory location. LB=lower bound,
UB=upper bound
Example: Consider the array of order 3 x 3.

******************************************************************

You might also like