Lect 03 CSE1201 Arrays
Lect 03 CSE1201 Arrays
CSE 1201
Data Structure
Week 2
Lecture 3
Chapter 2:
Arrays, Pointers and Records
2
Introduction
• Arrays
– Structures of related data items
– Static entity (same size throughout program)
• A few types
– Pointer-based arrays (C-like)
– Arrays as objects (C++)
3
Introduction
• Array
– Consecutive group of memory locations
– Same name and type (int, char, etc.)
• To refer to an element
– Specify array name and position number (index)
– Format: arrayname[ position number ]
– First element at position 1 (0 for C)
• N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
Array Creation
Topic 1: Write an Algorithm to insert n elements in an array
n: total elements
Start
x: input variable
Array Elements: a[0]…..a[n-1]
i ←0
i←i+1 F
i<n int main(){
T
int i, a[100], n;
Input x
n=10;
end
for(i=0;i<n;i++){
a[i] ← x
scanf(“%d”,&a[i]);
a[i]=x;
}
a[] 10 32 45
}
0 1 2 3 ….. …. n-2 n-1
←
4
Array Insertion
Topic : Insert a new element at index m.
n: total elements
Start
m: index 0 m n-1
x: input variable for new data
Input x, m Array Elements: a[0]…..a[n-1]
0 m n- F
1?
T
Shifting Required
i ←n-1 all elements from index m to
i← i-1 F
mi (n-1) are needed to be shifted to
T index (m+1) to n respectively.
a[i+1]← a[i] Total (n-m) elements to be
a[m] ← x shifted.
end
a[] 10 32 45
5
0 1 …. m … n-1 n
Array Deletion
Topic : Delete an specific element x
Start
n: total elements
Input x m: index 0 m n
x: input variable to be deleted
Array Elements: a[0]…..a[n-1]
i ←0
i←i+1 F
i<n
T
F
a[i]=x
Not Shifting Required
Found
T all elements from index
j ←i (m+1) to (n-1) are needed to
j ← j+1 F
be shifted from index m to
j n- end
2 T (n-2) respectively. Total (n-
a[j]← a[j+1]
m-1) elements to be shifted.
a[] 10 32 45
0 1 …. m … n-1 n
Bubble Sort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
"Bubbling Up" the Largest Element
1 2 3 4 5 6
77 42 35 12 101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 Swap77
42 35 12 101 5
77
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
42 35 12 77 5 101
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
Bubble Sort
Topic 5: Write an algorithm to sort an array using Bubble Sort.
Start
Input
a[0…n] #include <stdio.h>
#include <stdlib.h>
int main()
i ←0 {
i←i+1 F
i < n-1 int a[6]={10,3,41,12,77,21};
T int n=6;
j ←0 end int i,j,t;
j ← j+1 for(i=0;i<n-1;i++)
j n-2- for(j=0;j<=n-2-i;j++)
i)
T if(a[j]>a[j+1])
a[j]>a[j+1] {
F t=a[j];a[j]=a[j+1];a[j+1]=t;
T
}
a[j] a[j+1] for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
n: total elements
Array Elements: a[0]…..a[n-1] Corresponding C program
Two-dimensional Arrays
Matrix
In computer programming, a matrix can be defined with a 2-dimensional
array. Any array with 'm' columns and 'n' rows represents a mXn matrix.
Sparse Matrix
There may be a situation in which a matrix contains more number of ZERO
values than NON-ZERO values. Such matrix is known as sparse matrix
Example
consider a matrix of size 100 X 100 containing only 10 non-zero elements. In this
matrix, only 10 spaces are filled with non-zero values and remaining spaces of
matrix are filled with zero. That means, totally we allocate 100 X 100 X 2 = 20000
bytes of space to store this integer matrix. And to access these 10 non-zero
elements we have to make scanning for 10000 times.
Sparse Matrix
Representation
Triplet representation
Linked representation
Triplet Representation
In this representation, we consider only non-zero values along with their row and
column index values. In this representation, the 0 th row stores total rows, total
columns and total non-zero values in the matrix. consider a matrix of size 5 X 6
containing 6 number of non-zero values. This matrix can be represented as shown
in the image.
Sparse Matrix
Topic 5: Algorithm for Triplex Representation
mxn: total elements
Start Array: a[0…n-1,0…m-1] Int main(){
s[0…N, 0..2] int a[5][6], s[100][3];
k← 0 i: stores row of non-zero value
int k=0,n=5,m=6,i,j;
j: store col of non-zero value
for(i=0;i<n;i++){
i ←0 k: counter for non-zero values
for(j=0;j<m;j++)
i←i+1 F
i<n if(a[i][j]!=0){
T k++;
j ←0 s[0][0] ← n
s[k][0]=I;
j ← j+1 s[0][1] ← m
j < m) s[0][2] ← k s[k][1]=j;
T s[k][2]=a[i][j];
a[i][j]=0 end }
T
F }
k←k+1 s[0][0]=n; s[0][1]=m; s[0][2]=k;
s[k][0] ←i
}
s[k][1] ←j
s[k][2] ←a[i][j]
Corresponding C program
Searching
Search: locate an item in a list of data/information.
i ←0 int main(){
i←i+1 F
i<n Int x,i;
T for(i=0;i<n;i++){
a[i]=x if(a[i]==x)
F end
T
printf(“%d”,i);
Print i }
}
←
a[] 10 32 45
0 1 2 3 ….. …. n-2 n-1
2
6
Linear Search
Algorithm:
Input: Array, #elements, item (to search)
Start with the element at index = 0
Step 1: Compare the element at index with item. If its equal to item then return index
with status “Found” otherwise go to step 2.
Step 2: Increase index by 1. If index is less than #elements go to step 1 otherwise
return -1 with status “Not found”.
indexindexindexindexindexindex
value 15
found false
true
0 1 2 3 4 5 6 7 8 9
30 62 20 100 77 15 90 52 88 45 position -1
5
Linear Searching
Topic 3: Modifications of previous algorithm
Start
n: total elements
Input x x: input variable
flag: a variable
flag ← 0 Array Elements: a[0]…..a[n-1]
i ←0
i←i+1 F
i<n F
T flag=1
F
a[i]=x T Not
T Found
Print i
end
flag ← 1
Complexity
O(1) for best-case 2
O(n) for worst-case 8
Binary Search
Suppose an array ax[] has 10 elements
beg 0
6
5
end 9
6
4 ax[] 10 22 25 44 55 66 77 79 92 99
mid 7
5
6
4 index 0 Simulation
1 2 3 4 5 6 7 8 9
found
not found mid mid mid mid
Flowchart: Binary Search
At Iteration 1:
Length of array = n
At Iteration 2:
Length of array = n/2
At Iteration 3:
Length of array = (n/2)/2 = n/22
Therefore, after Iteration k:
Length of array = n/2k
Also, we know that after k iterations,
the length of the array becomes
1 Therefore, the Length of the array
n/2k = 1
=> n = 2k
Applying log function on both sides:
=> log2n = log22k
=> log2n = k * log22
As (loga (a) = 1) Therefore, k 30
= log2(n)
Time Complexity=O(log2n)
Binary Search Program
#include <iostream> int main(void) {
using namespace std; int array[] = {3, 4, 5, 6, 7, 8, 9};
int binarySearch(int array[], int x, int low, int high) int x = 7;
{
//int n = sizeof(array) / sizeof(array[0]);
// Repeat until the pointers low and high meet
n=7;
each other
int result = binarySearch(array, x, 0, n - 1);
while (low <= high) {
if (result == -1)
int mid = low + (high - low) / 2;
printf("Not found");
if (array[mid] == x)
else
return mid;
printf("Element is found at index %d",
if (array[mid] < x)
result);
low = mid + 1;
}
else
high = mid - 1;
}
return -1;
}
31
Q1: An array is what kind of data
structure?
A Linear
B Non-linear
C Sorted
D Sequential ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q2: If a new element is needed to
be stored at index 5 of 100
elements array then how many
elements should be shifted?
A5
B 100
C 95
D 96 ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q3: If an element is needed to be
deleted with index 10 of 100
elements array then how many
elements should be shifted?
A 10
B 90
C 89
D 110 ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q4: How many times bubble up
is repeated in Bubble Sort
algorithm with 10 elements?
A 10
B 11
C9
D 12 ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q5: The complexity of
Bubble Sort algorithm is
_____?
A O (n)
B O (n-1)
C O (2n)
D O (n )
2
ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q6: The sparse matrix
has more _____ values?
A zero
B non-zero
C positive
D negative ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q7: Which searching can
be performed with sorted
data?
A Binary Search
B Linear Search
C
D ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q9: The complexity of
Binary search is _____?
A O(n)
B O(2n)
C O(n2)
D O(log2n)
ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q10: Identify the following
________.?
First Commercial Processor Price US$60
A Game Controller
Launched November 15, 1971; 50
years ago
B Microprocessor
Discontinued 1981
Common manufacturer(s) •Intel
Performance
C ROM Chip
Max. CPU clock rate
Data width
740-750 kHz
4 bits
Display
D Controller
Address width 12 bits (multiplexed)
ST
2
8
3
1
16
14
5
18
9
23
11
19
7
27
25
30
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Assignments 42
End of Chapter 2