Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

ARRAYS

2.1 Introduction

 Data Structure can be classified as:


 linear
 non-linear
 Linear (elements arranged in sequential in
memory location) i.e. array & linear link-list
 Non-linear such as a tree and graph.
 Operations:
 Traversing, Searching, Inserting, Deleting,
Sorting, Merging
 Array is used to store a fix size for data and a
link-list the data can be varies in size.

2
2.1 Introduction

 Advantages of an Array:
 Very simple
 Economy – if full use of memory
 Random accessed at the same time

 Disadvantage of an Array:
 wasting memory if not fully used

3
2.2 Linear Array

 Homogeneous data:
a) Elements are represented through indexes.
b) Elements are saved in sequential in memory locations.
 Number of elements, N –> length or size of an array.
If:
UB : upper bound ( the largest index)
LB : lower bound (the smallest index)
Then: N = UB – LB + 1
Length = N = UB when LB = 1
4
5
2.2.1 Representation of Array in a Memory

 The process to determine the address in a memory:


a) First address – base address.
b) Relative address to base address through index function.

Example: char X[100];


Let char uses 1 location storage.
If the base address is 1200 then the next element is in 1201.
Index Function is written as:
Loc (X[i]) = Loc(X[0]) + i , i is subscript and LB = 0

1200 1201 1202 1203

X[0] X[1] X[2]

6
7
 LOC (LA [K]) = Base (LA) + w (K – LB)
Here LOC (LA [K]) is the location of the Kth element of LA.
Base (LA) is the base address of LA.
w is the number of bytes taken by one element.
K is the Kth element.
LB is the lower bound.
 Suppose we want to find out Loc (A [3]). For it, we have:
Base (A) = 1000
w = 2 bytes (Because an integer takes two bytes in the memory).
K=3
LB = 1

After putting these values in the given formula, we get:


LOC (A [3]) = 1000 + 2 (3 – 1)
= 1000 + 2 (2)
= 1000 + 4
= 1004

8
2.2.1 Representation of Array in a Memory

 w is length of memory location required.


For real number: 4 byte, integer: 2 byte and character: 1 byte.

 Example:
If LB = 5,
Loc(X[LB]) = 1200,
and w = 4, find Loc(X[8]) ?
Loc(X[8])= Loc(X[5]) + 4*(8 – 5)
= 1212
9
2.2.2 Traversing Algorithm

 Traversing operation means visit every element once.


e.g. to print, etc.
 Here Arr is a linear array with lower bound L
and upper bound U
 Algorithm:

1.[Initialize Counter] Set K=L.


2.Repeat steps 3 and 4 while K<=U
3.Print Arr[K].
4.[Increase Counter] Set K=K+1.
5.Exit

10
Static vs Dynamic Arrays
 A STATIC ARRAY is declared at compile time; thus, its size must be
a compile time constant. FORTRAN, COBOL and Pascal, use this
technique exclusively. These cannot have variable sizes because
allocation of the array is done at compile-time. These are usually
placed on the stack. No re-sizing is allowed.
 A DYNAMIC ARRAY may be declared at compile-time but it is not
dimensioned (the number of elements determined) until run-time.
Thus, the value that determines the size may come from a constant or
variable. Furthermore, the array may be allocated and then de-
allocated multiple times and with different sizes. Java are languages
that only allow dynamic arrays.
 Several languages, such as C, C++ and several forms of BASIC, allow
both.
2.2.3 Insertion Algorithm
2.2.3 Insertion Algorithm

 Example algorithm:
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −

INSERT(LA, N, K, ITEM)
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 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
14
2.2.4 Deletion Algorithm
2.2.4 Deletion Algorithm
 Algorithm: Deletion refers to removing an existing element from the
array and re-organizing all elements of an array.
 Consider LA is a linear array with N elements and K is a positive
integer such that K<=N. Following is the algorithm to delete an
element available at the Kth position of LA.
DELETE(LA, N, K, ITEM)
1. Start
2. Set J = K
3. Repeat steps 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 16
2.2.5 Linear Search
 Linear search is a very basic and simple search algorithm. In Linear search,
we search an element or value in a given array by traversing the array
from the starting, till the desired element or value is found.
 It compares the element to be searched with all the elements present in
the array and when the element is matched successfully, it returns the
index of the element in the array, else it return -1.
 Linear Search is applied on unsorted or unordered lists, when there are
fewer elements in a list.
 Features of Linear Search Algorithm
1. It is used for unsorted and unordered small list of elements.
2. It has a time complexity of O(n), which means the time is linearly
dependent on the number of elements, which is not bad, but not that
good too.
3. It has a very simple implementation.
17
 Linear Search ( Array A, Value x)
Step 1: i=1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: i=i+1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
2.2.5 Binary Search Algorithm
Binary Search is applied on the sorted array or list of large size. It's time complexity
of O(log n) makes it very fast as compared to other sorting algorithms. The only
limitation is that the array or list of elements must be sorted for the binary search
algorithm to work on it.
Following are the steps of implementation that we will be following:
 Start with the middle element:
 If the target value is equal to the middle element of the array, then return the
index of the middle element.
 If not, then compare the middle element with the target value,
 If the target value is greater than the number in the middle index, then pick
the elements to the right of the middle index, and start with Step 1.
 If the target value is less than the number in the middle index, then pick the
elements to the left of the middle index, and start with Step 1.
 When a match is found, return the index of the element matched.
19
 If no match is found, then return -1
2.2.5 Binary Search Algorithm

Features of Binary Search


 It is great to search through large sorted arrays.
 It has a time complexity of O(log n) which is a very good
time complexity.
 It has a simple implementation.

20
2.2.5 Binary Search Algorithm
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <=END
Step 3: SET MID = (BEG + END)/2

Step 4: IF A[MID] = VAL


SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE IF A[MID] < VAL
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]

Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT 21
Merge of two sorted arrays
2.2.6 Merging
 Suppose
Algorithm
1) A is a sorted list with R elements
2) B is a sorted list with S elements.
3) The operation that combines the element of A and B into a single sorted
list C with N=R + S elements is called merging.

24
2.2.6 Merging Algorithm
 Algorithm: Here A and B be sorted arrays with R and S elements
respectively. This algorithm merges A and B into an array C with N=R+ S
elements.
Merging (A, R,B,S,C)  Step 4: If NA >R, then:
 Step 1: Start Repeat while NB ≤ S:
 Step 2:Set NA=1, Set C[NC] = B[NB]
 NB=1 Set NB = NB+1
 NC=1 Set NC = NC +1
 Step 3: Repeat while NA ≤ R and NB ≤ S: [End of Loop]
else
if A[NA] ≤ B[NB], then: Repeat while NA ≤ R:
Set C[NC] = A[NA] Set C[NC] = A[NA]
Set NA = NA +1 Set NC = NC + 1
else Set NA = NA +1
Set C[NC] = B[NB] [End of loop]
Set NB = NB +1 [End of if structure]
[End of if structure]  Step 5: Return C[NC]
Set NC= NC +1  Step 6: Stop
[End of Loop]

26
2.2.6 Merging Algorithm
 Complexity of merging: The input consists of the total number n=r+s elements in A
and B. Each comparison assigns an element to the array C, which eventually has n
elements. Accordingly, the number f(n) of comparisons cannot exceed n:
f(n) ≤ n = O(n)

27
Bubble Sort

 Bubble sort, sometimes referred to as sinking sort, is a


simple sorting algorithm that repeatedly steps through
the list, compares adjacent pairs and swaps them if
they are in the wrong order. The pass through the list is
repeated until the list is sorted.
 VIDEO DEMO OF BUBBLE SORT
Step 1: Repeat Steps 2 and 3 for i=2 to n
Step 2: Set j=1
Step 3: Repeat while j<=n
(A) if a[i] < a[j]
Then interchange a[i] and a[j]
[End of if]
(B) Set j = j+1
[End of Inner Loop]
[End of Step 1 Outer Loop]
Step 4: Exit
Insertion Sort
CHARACTERISTICS OF INSERTION SORT:
 It is efficient for smaller data sets, but very inefficient for larger lists.
 Insertion Sort is adaptive, that means it reduces its total number of steps if a partially
sorted array is provided as input, making it efficient.
 It is better than Selection Sort and Bubble Sort algorithms.
 It is a stable sorting technique, as it does not change the relative order of elements
which are equal.
Insertion sort is a simple sorting algorithm that works
the way we sort playing cards in our hands.
9 exit
Selection Sort

 The selection sort algorithm sorts an array by


repeatedly finding the minimum element
(considering ascending order) from unsorted part
and putting it at the beginning. The algorithm
maintains two subarrays in a given array.
1)The subarray which is already sorted.
2) Remaining subarray which is unsorted.
 In every iteration of selection sort, the minimum
element (considering ascending order) from the
unsorted subarray is picked and moved to the sorted
subarray.
can you convert it into
algorithm?

You might also like