Arrays
Arrays
UCS301
Array
Department of CSE
Thapar Institute of Engineering and Technology, Patiala
Array ADT float marks[10];
• Usage:
– Used frequently to store relatively permanent
collections of data.
– Not suitable if the size of the structure or the
data in the structure are constantly changing.
Memory Storage
Memory Storage – One Dimensional Array
student[1] student[3]
marks[1]
Memory Storage – Two Dimensional Array
int marks[3][5];
• Column-major order
(0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2) (2,2) (0,3) (1,3) (2,3) (0,4) (1,4) (2,4)
Row-major 1 [0] 1 2 3 4
M=3
2 [1] 5 6 7 8
N=4
3 [2] 9 10 11 12
4
5
6 Address of A[2][1] =
7 B + W * (4 * (2 – 0) + (1 – 0))
8
9
10 Address of A[I][J] =
11 B + W * ( N * ( I – LBR ) + ( J – LBC ))
12
[0] [1] [2] [3]
[0] 1 2 3 4 1 Column-major
M=3
[1] 5 6 7 8 5
N=4
[2] 9 10 11 12 9
2
Address of A [2][1] = 6
10
B + W * ((2 – 0) + 3 * (1
(2 – 0))
3
7
11
Address of A [I][J] = 4
B + W * ((I – LBR) + M * (J – LBC)) 8
12
Contd…
• Row Major
Address of A[I][J] = B + W * ( N * ( I – LBR ) + ( J – LBC ))
• Column Major
Address of A [I][J] = B + W * (( I – LBR ) + M * ( J – LBC ))
Algorithm arrayMinElement(A,n)
Input: An array A containing n integers.
Output: The minimum element in A.
1. min = 0
2. for i = 1 to n-1 do
3. if A[min] > A[i] 1. int arrayMinElement(int arr[], int n)
4. min = i 2. { int min = 0;
5. return A[min] 3. for (int i = 1; i < n; i++)
4. { if (arr[i] < arr[min])
5. min = i;
6. }
7. return arr[min];
8. }
Search
Deletion
Delete an element from the array
Insertion and Deletion
0 1 2 3 4 5 6 7 8 9
a[] 8 6 3 4 5
• Insert 2 at index 1
4 3 2 1
0 1 2 3 4 5 6 7 8 9
a[] 8 6 3 4 5
2 6 3 4 5
• Delete the value at index 2
1 2 3
0 1 2 3 4 5 6 7 8 9
a[] 8 2 6 3 4 5
3 4 5
Algorithm – Insertion
Algorithm insertElement(A,n,num,indx)
Input: An array A containing n integers and the
number num to be inserted at index indx.
Output: Successful insertion of num at indx.
1. for i = n – 1 to indx do
2. A[i + 1] = A[i]
1. void insert(int a[], int num, int pos)
3. A[indx] = num 2. { for(int i = n-1; i >= pos; i--)
4. n = n + 1 3. a[i+1] = a[i];
4. a[pos] = num;
5. n++;
6. }
Algorithm – Deletion
Algorithm deleteElement(A,n,indx)
Input: An array A containing n integers and the index
indx whose value is to be deleted.
Output: Deleted value stored initially at indx.
1. temp = A[indx]
2. for i = indx to n – 2 do
1. int deleteElement(int a[], int pos)
3. A[i] = A[i + 1] 2. { int temp = a[pos];
4. n=n–1 3. for(int i = pos; i <= n-2; i++)
4. a[i] = a[i+1];
5. return temp 5. n--;
6. return temp;
7. }
Bubble Sort
Bubble Sort – Ascending
[0]
8 1 1 1 1 1 1 1
[1] 1 8 7 7 7 7
5 5
2 2
[2] 7 7 8 5 5 5
7
2 2
5 5
[3] 5 5 5 8 2 2
7 7 7
[4] 2 2 2 2 8 8 8 8
Pass 1st 2nd 3rd 4th
Bubble Sort – Descending
[0]
8 8 8 8 8 8 8 8
[1] 1 1 1 7 7 7 7 7
[2] 7 7 7 1 1 5 5 5
[3] 5 5 5 5 5 1 2 2
[4] 2 2 2 2 2 2 1 1
Pass 1st 2nd 3rd 4th
Algorithm – Bubble Sort
Algorithm bubbleSort(A,n)
Input: An array A containing n integers.
Output: The elements of A get sorted in increasing order.
1. for i = 1 to n – 1 do
2. for j = 0 to n – i – 1 do
[0]
5 1 1 1 1 1
[1] 1 5 2 2 2 2
[2] 2 2 5 3 3 3
[3] 3 3 3 5 4 4
[4] 4 4 4 4 5 5
Pass 1st 2nd
Algorithm – Optimized Bubble Sort
Algorithm bubbleSortOpt(A,n)
Input: An array A containing n integers.
Output: The elements of A get sorted in increasing order.
1. for i = 1 to n – 1 The best case
2. flag = true complexity reduces
3. for j = 0 to n – i – 1 do to the order of n,
4. if A[j] > A[j + 1] but the worst and
5. flag = false average is still n2.
6. Exchange A[j] with A[j+1] So, overall the
7. if flag == true complexity is of the
8. break; order of n2 again.
Sparse Matrix
Sparse Matrix
• A matrix is sparse if many of its elements are zero.
• A matrix that is not sparse is dense.
• Two possible representations
– Array (also known as triplet) 0 0 0 2
0 6 0 0
– Linked list
0 0 0 9
0 5 4 0
Array representation
Row Col Value
[0] [1] [2] [3] [4] [5]
6 6 8
[0] 15 0 0 22 0 -15 0 0 15
[1] 0 11 3 0 0 0 0 3 22
0 5 -15
[2] 0 0 0 -6 0 0
1 1 11
[3] 0 0 0 0 0 0 1 2 3
[4] 91 0 0 0 0 0 2 3 -6
4 0 91
[5] 0 0 28 0 0 0
5 2 28
Operations
• Transpose
• Addition
• Multiplication
Transpose
[3] 22 0 -6 0 0 0
[4] 91 0 0 0 0 0
[5] -15 0 28 0 0 0
Row Col Value
Addition Counter = 14
6 6 14
0 0 30
0 3 22
Row Col Value Row Col Value 0 4 91
6 6 8 6 6 8 0 5 -15
0 0 15 0 0 15 1 1 22
0 3 22 0 4 91 1 2 3
0 5 -15 1 1 11 2 1 3
1 1 11 2 1 3 2 3 -6
1 2 3 2 5 28 2 5 28
3 0 22
2 3 -6 3 0 22
3 2 -6
4 0 91 3 2 -6
4 0 91
5 2 28 5 0 -15
5 0 -15
5 2 28
Multiplication
• Compute A x B
• First take transpose of B.
• Multiply only if the corresponding elements are
present and add them for each position in the
resultant matrix.
Multiplication
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 8 0 [0]
[1] 0 0 0 0
× [1] 0 0 0 23
= [1]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0]
[1] 0 0 0 0
× [1] 0 0 0 25
= [1]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0] 240
[1] 0 0 0 0
× [1] 0 0 0 25
= [1]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0] 240 300
[1] 0 0 0 0
× [1] 0 0 0 25
= [1]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0] 240 300 0
[1] 0 0 0 0
× [1] 0 0 0 25
= [1]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0] 240 300 0 230
[1] 0 0 0 0
× [1] 0 0 0 25
= [1]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0] 240 300 0 230
[1] 0 0 0 0
× [1] 0 0 0 25
= [1] 0
[2] 0 0 5 0 [2] 8 0 9 0 [2]
[0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [2] [3]
[0] 0 10 0 12 [0] 0 0 0 20 [0] 240 300 0 230
[1] 0 0 0 0
× [1] 0 0 0 25
= [1] 0 0 0 0
[2] 0 0 5 0 [2] 8 0 9 0 [2] 0 0 45 0
[3] 15 12 0 0 [3] 0 23 0 0 [3] 0 0 120 276
Multiplication Counter = 0
Row Col Value Row Col Value Row Col Value
4 4 5 4 4 5 4 4
0 1 10 0 3 20
0 3 12 1 3 25
2 2 5 2 0 8
3 0 15 2 2 9
3 1 12 3 1 23
Row Col Value
4 4 5
0 2 8
1 3 23
2 2 9
3 0 20
3 1 25
Multiplication Counter = 6