2. Array
• An array is a collection of homogeneous type of data
elements.
• An array is consisting of a collection of elements .
• Operation Performed On Array:
1. Traversing
2. Search
3. Insertion
4. Deletion
5. Sorting
6. Merging
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
1
2
3
4
5
Representation of array
3. Linear Arrays
• A linear array is the list of finite number ‘N’ of homogeneous
data elements (i.e. data elements of same type)
• Length = UB – LB + 1
• Array A may be denoted by,
– A1, A2, A3, … AN - Subscript Notation
– A(1), A(2), A(3), …., A(N) – Fortran, PL/I, BASIC
– A[1], A[2], A[3],..,A[N] – PASCAL, C, C++, Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
4. Traversing
• This algorithm traverses a linear array A with lower bound LB
and upper bound UB.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
//Using for loop
for k := LB to UB
Process -> LA [k];
//Using while loop
k := LB
while(k <= UB)
{
Process -> LA[k];
k:= k + 1;
}
5. Inserting
22
23
26
28
29
31
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
LA[1]
LA[2]
LA[3]
LA[4]
LA[5]
LA[6]
LA[7]
22
23
25
26
28
29
31
Algorithm Insert(LA, N, K, ITEM)
{
J := N; //Initialize counter
while(J >= K)
{
LA[J + 1] := LA [J]; // Move one elements downward
J := J - 1; // Decrease counter by 1
}
LA[K] := ITEM // Insert element
N := N + 1; // Reset N
}
6. Deleting
Here LA is a linear array with N elements.
LOC is the location where ITEM is to be deleted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm Delete(LA, N, LOC, ITEM)
{
ITEM := LA[LOC] // Assign the elements to be deleted
for J := LOC to N do J := J + 1
{
LA[J] := LA [J + 1]; // Move Jth element upwards
}
N := N - 1; // Reset N
}
7. Searching Algorithms
• Linear Search
– A linear search sequentially moves through your collection (or data
structure) looking for a matching value.
– Worst case performance scenario for a linear search is that it needs to
loop through the entire collection; either because the item is the last
one, or because the item isn't found.
– In other words, if you have N items in your collection, the worst case
scenario to find an item is N iterations. This is known as O(N) using the
Big O Notation.
– Linear searches don't require the collection to be sorted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
8. Linear Search
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm LinearSearch (LA, N, ITEM)
{
for J := 1 to N do J := J + 1
{
if(ITEM = LA [j]) then
{
write (ITEM +" found at location " +J);
Return;
}
}
if(J > N) then
{
write (ITEM +" does not exist.");
}
}
55 88 66 77 99 11 6
J = 1 J = 2 J = 3
= 6= 6 = 6= 6 = 6= 6 6 found at location 3
9. Searching Algorithms
• Binary Search
– Binary search relies on a divide and conquer strategy to find a value
within an already-sorted collection.
– The algorithm is deceptively simple.
– Binary search requires a sorted collection.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
15. Bubble Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm Bubble (DATA, N)
{
for K : = 1 to N -1 do K := K + 1
{
PTR : = 1;
While(PTR <= N - K)
{
if(DATA [PTR] > DATA [PTR +1]) then
Interchange (DATA [PTR] and DATA [PTR +1])
PTR := PTR +1;
}
}
}
16. Insertion Sort
• Suppose an array A with N elements, the insertion sort algorithm scans A
from 1 to N, inserting each element A [K] into its proper position in the
previously sorted sub-array A[1], A [2] , …, A[K-1].
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
77 33 44 11 88 22 66 55
77 33 44 11 88 22 66 55
33 77 44 11 88 22 66 55
33 44 77 11 88 22 66 55
11 33 44 77 88 22 66 55
11 33 44 77 88 22 66 55
11 22 33 44 77 88 66 55
11 22 33 44 66 77 88 55
11 22 33 44 55 66 77 88
Pass
K = 1
K = 2
K = 3
K = 4
K = 5
K = 6
K = 7
K = 8
Sorted
1 2 3 4 5 6 7 8
17. Insertion Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm INSERTION (DATA, N)
{
for K:= 1 to N do K := K + 1
{
TEMP := DATA [K];
PTR := K - 1;
while (PTR >= 0 && TEMP < DATA [PTR])
{
DATA [PTR + 1] := DATA [PTR];
PTR = PTR - 1;
}
DATA [PTR + 1]:= TEMP;
}
}
18. Selection Sort
• Selection sort algorithm for sorting A works as follows:
– First find the smallest element in the list and put it in the
first position.
– Then find the second smallest element in the list and put it
in the second position.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
19. Selection Sort
Suppose an array A with 8 elements as follows:
77, 33, 44, 11, 88, 22, 66, 55.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
77 33 44 11 88 22 66 55
11 33 44 77 88 22 66 55
11 22 44 77 88 33 66 55
11 22 33 77 88 44 66 55
11 22 33 44 88 77 66 55
11 22 33 44 55 77 66 88
11 22 33 44 55 66 77 88
11 22 33 44 55 66 77 88
Pass
K = 1
K = 2
K = 3
K = 4
K = 5
K = 6
K = 7
1 2 3 4 5 6 7 8
LOC = 4
LOC = 6
LOC = 6
LOC = 6
LOC = 8
LOC = 7
LOC = 7
Sorted
20. Selection Sort
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm SELECTION (A, N)
{
for J := 1 to N do J := J + 1
{
MINI(A, J, N, LOC);
[Interchange A[J] and A[LOC]];
TEMP := A[J];
A[J] := A[LOC];
A[LOC] := TEMP;
}
}
MINI(A, K, N, LOC)
{
MIN := A[K] and LOC := K;
for J := K + 1 to N do J := J + 1
{
if (MIN > A [J]) then
{
MIN := A [J];
LOC := J;
}
}
Return LOC;
}