4/27/2026
Arrays Traversing a Linear Array
Lecture 2
The array is the most commonly used Algorithm 4.1: (Traversing a linear Array) Here LA is a
Data Structures and Algorithms linear array with lower bound LB and Upper bound UB.
data storage structure; it's built into most This algorithm traverses LA applying an operation
programming languages. PROCESS to each element of LA.
Arrays Because they are so well known, arrays 1. [Initialize counter.] Set K:=LB.
offer a convenient jumping off place for 2. Repeat Step 3 and 4 while K UB.
introducing data structures and for 3. [Visit elements.] Apply PROCESS to LA[K].
4. [Increase Counter.] Set K:= K+1
seeing how object-oriented programming
[End of Sep 2 Loop]
and data structures relate to each other. 5. Exit
BUITEMS SE-Spring 2026 BUITEMS SE-Spring 2026 BUITEMS SE-Spring
Arrays 1 Arrays 2 2026 Arrays 3
1 2 3
Insertion in Array Deletion in Array Linear Search
Algorithm 4.2: Algorithm 4.3: (Deleting from a Linear Array) DELETE(LA, N, K, ITEM) Algorithm 4.5: (Linear Search) LINEAR(DATA, N, ITEM, LOC)
(Inserting into a Linear Array) INSERT(LA,N,K,ITEM)
Here LA is the linear array with N elements and K is
Here LA is a linear array with N elements and K is a positive Here DATA is Linear Array with N elements,
a positive integer such that K<=N. This algorithm
integer such that K<=N. This algorithm deletes the and ITEM is a give item of information. This
Kth element from LA. Algorithm finds the location LOC of ITEM in
inserts an element ITEM into the Kth position in LA.
DATA, or sets LOC:=0 if the search is
1. Set ITEM :=LA[K]. unsuccessful.
1. [initialize counter.] Set J:=N
2. Repeat Step 3 and 4 while JK 2. Repeat for J=K to N-1: 1 [Insert ITEM at the end of DATA.] Set DATA[N+1]:=ITEM.
3. [Move Jth element downward.] Set LA[j+1]:=LA[j]. [Move J+1st Element upward.] Set LA[J] = LA[J+1]. 2 [Initialize counter.] Set LOC = 1.
4. [Decrease counter.] Set J:= j-1. [End of loop] 3 Repeat while DATA[LOC] ITEM:
[End of Step 2 Loop] Set LOC := LOC+1
5. [Insert element.] Set LA[K] := ITEM. 3. [Reset number N of elements in LA.] Set N:=N-1. [End of loop.]
6. [Reset N.] Set N:=N+1. 4. Exit 4 [Successful?] if LOC = N+1, then : Set LOC :=0.
7. Exit 5 Exit.
BUITEMS SE-Spring BUITEMS SE-Spring 2026 BUITEMS SE-Spring
2026 Arrays 4 Arrays 5 2026 Arrays 6
4 5 6
1
4/27/2026
Binary Search Binary Search
1. [Initialize Segment Variables.]
Set BEG:= LB, END:= UB and MID:=(INT(BEG+END)/2).
2.
3.
Repeat Steps 3 and 4 while BEG<END and DATA[MID]! =ITEM.
if ITEM<DATA[MID], than:
The End
Set END := MID-1.
Else:
Set BEG:= MID+1
[End of If structure.]
4. Set MID := (INT(BEG+END)/2).
Algorithm 4.- (Binary Search) BINARY(DATA, LB, UB, ITEM, LOC) [End of Ste2 loop.]
Here DATA is a sorted array with lower bound LB and uppers 5. If( DATA[MID] = ITEM, than:
bound UB, and ITEM is given item of information. The variables Set Loc := MID
BEG, END and MID denote respectively, the beginning, end a Else:
middle locations of a segment of elements of DATA. This Set Loc := NULL.
algorithms finds the location LOC of ITEM in DATA or sets [End of If structure.]
DATA=NULL
6. Exit
BUITEMS SE-Spring BUITEMS SE-Spring BUITEMS SE-Spring
2026 Arrays 7 2026 Arrays 8 2026 Arrays 9
7 8 9