0% found this document useful (0 votes)
49 views17 pages

Arrays

The document provides an overview of linear arrays as a data structure. It defines linear arrays as lists of homogeneous data elements that are referenced using consecutive integer indices and stored in successive memory locations. The key topics covered include the memory representation of arrays, traversing arrays using for and while loops, inserting and deleting elements from arrays, searching arrays using linear and binary search, and sorting arrays using bubble sort. Worked examples are provided for various array operations.

Uploaded by

Ankit Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views17 pages

Arrays

The document provides an overview of linear arrays as a data structure. It defines linear arrays as lists of homogeneous data elements that are referenced using consecutive integer indices and stored in successive memory locations. The key topics covered include the memory representation of arrays, traversing arrays using for and while loops, inserting and deleting elements from arrays, searching arrays using linear and binary search, and sorting arrays using bubble sort. Worked examples are provided for various array operations.

Uploaded by

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

Data Structures

Lecture 5: Linear Array

By
Ravi Kant Sahu
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Linear Array
• Memory Representation of Linear Array
• Traversing Array
• Insertion and Deletion in Array
• Sorting (Bubble Sort)
• Searching (Linear Search and Binary Search)
• Review Questions

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Basic Terminology
• Linear Data Structures: A data structure is said to
be linear if its elements form a sequence or a linear list.

• Linear Array: is a list of a finite number n of


homogeneous data elements such that:
(a) the elements of the array are referenced by an index
set consisting of n consecutive numbers.
(b) the elements of the array are stored respectively in
successive memory locations.

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Key Terms
• Size / Length of Array
• Index of Array
• Upper bound and Lower bound of Array

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Memory Representation of Arrays
19 5 42 18 199

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Traversing Linear Array
• Suppose we have to count the number of element in
an array or print all the elements of array.

• Algorithm 1: (Using While Loop)

1. [Initialize Counter.] Set K = LB.


2. Repeat Step 3 and 4 while K<= UB.
3. [Visit Element.] Apply PROCESS to A[K].
4. [Increase Counter.] Set K = K+1.
[End of Step 2 Loop.]
5. Exit.
Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India
• Algorithm 2: (Using for loop)

1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.

Question.1: Find the Number of elements in an array which are


greater than 25.

Question 2: Find out the sum of all the two digit numbers in an
array.
Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India
Insertion and Deletion in an Array
• Two types of insertion are possible:
• Insertion at the end of array
• Insertion in the middle of the array

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Insertion into a Linear Array
• Algorithm: (Insertion of an element ITEM into Kth
position in a Linear Array A)

1. [Initialize Counter.] Set J = N.


2. Repeat Steps 3and 4 while J >= K.
3. [Move Jth element downward] Set A[J+1] = A[J].
4. [Decrease Counter.] Set J = J-1.
[End of Step 2 loop]
5. [Insert element.] Set A[K] = ITEM.
6. [Reset N] N = N+1.
7. Exit

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Deletion into a Linear Array
Algorithm: (Delete Kth element from Linear Array A)

1. Repeat for J = K to N-1.


2. [Move (J+1)th element upward] Set A[J] = A[J+1].
[End of loop.]
3. [Reset the number of elements N] Set N = N-1.
4. Exit

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Searching
1. Linear Search:
• Compares the item of interest with each element
of Array one by one.
• Traverses the Array sequentially to locate the
desired item.

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Linear Search Algorithm
• LINEAR_SEARCH (DATA, N, ITEM, LOC)

1. [Insert ITEM at the end of DATA] Set Data [N+1]= ITEM.


2. [Initialize Counter] Set LOC=1.
3. [Search for ITEM.]
Repeat while DATA [LOC] != ITEM
Set LOC = LOC +1.
[End of loop.]
4. [Successful?] if LOC = N + 1, then Set LOC = 0.
5. Exit.

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


2. Binary Search
• BINARY ( DATA, LB, UB, ITEM, LOC )
1. [Initialize Segment Variables]
Set BEG = LB, END = UB and MID = INT ((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG <= END and DATA [MID] != ITEM.
3. If ITEM < DATA[MID], then:
Set END = MID - 1.
Else:
Set BEG = MID + 1.
[End of if Structure.]
4. Set MID = INT ((BEG+END)/2).
[End of Step 2 Loop.]
5. If DATA [MID] = ITEM, then: Set LOC= MID
Else:
Set LOC = NULL.
[End of if structure.]
6. Exit. Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India
Limitations of Binary Search
• Although the complexity of Binary Search is
O (log n), it has some limitations:
1. the list must be sorted
2. one must have direct access to the middle
element in any sublist.

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Sorting (Bubble Sort)
• Sorting refers to the operation of rearranging the elements
of A so they are in some particular order.

• Complexity of Bubble Sort Algorithm is: O(n2)

• Example of Bubble Sort

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Bubble Sort Algorithm
Bubble (DATA, N)
1. Repeat Step 2 and 3 for K=1 to N-1.
2. [Initialize Pass Pointer P] Set P=1.
3. [Execute Pass] Repeat while P <= N-K.
(a) if DATA [P] > DATA [P+1], then:
Interchange DATA [P] and DATA[P+1]
[End of if Structure.]
(b) Set P = P+1.
[End of Inner Loop.]
[End of Step1 Outer Loop.]
4. Exit

Ravi Kant Sahu, Asst. Professor @ LPU Phagwara (Punjab) India


Questions

You might also like