Professional Documents
Culture Documents
3 Arrays
3 Arrays
3 Arrays
Data Structures
Lecture : Linear Array
Contents
• Basic Terminology
• Linear Array
• Memory Representation of Linear Array
• Traversing Array
• Insertion and Deletion in Array
• Sorting (Bubble Sort)
• Searching (Linear Search and Binary Search)
• Review Questions
Basic Terminology
• Linear Data Structures: A data structure is said to be linear if its
elements form a sequence or a linear list.
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
Traversing Linear Array
• Suppose we have to count the number of element is an array
or print all the elements of array.
1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.
Question 2: Find out the sum of all the two digit numbers in an
array.
Insertion and Deletion in an Array
• Two types of insertion are possible:
• Insertion at the end of array
• Insertion in the middle of the array
Insertion into a Linear Array
• Algorithm: (Insertion of an element ITEM into Kth position in
a Linear Array A)
11
Merging Algorithm
Algorithm: Merging (A, R,B,S,C)
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
Step 1: Set NA=0, NB=0 and NC=0
Step 2: Repeat while NA < R and NB < S:
if A[NA] ≤ B[NB], then:
Set C[NC] = A[NA]
Set NA = NA +1
else
Set C[NC] = B[NB]
Set NB = NB +1
[End of if structure]
Set NC= NC +1
[End of Loop]
12
Merging Algorithm
Step 3: If NA >=R, then:
Repeat while NB < S:
Set C[NC] = B[NB]
Set NB = NB+1
Set NC = NC +1
[End of Loop]
else
Repeat while NA < R:
Set C[NC] = A[NA]
Set NC = NC + 1
Set NA = NA +1
[End of loop]
[End of if structure]
Step 4: Return C[NC]
13
#include <stdio.h>
Merge Program
int main() {
void merge_arrays(int A[], int R, int B[], int S, int C[]) {
int NA = 0, NB = 0, NC = 0; int A[] = {1, 3, 5, 7, 9};
int R = sizeof(A) / sizeof(A[0]);
while (NA < R && NB < S) {
if (A[NA] <= B[NB]) {
C[NC] = A[NA]; int B[] = {2, 4, 6, 8, 10};
NA++; int S = sizeof(B) / sizeof(B[0]);
} else {
C[NC] = B[NB];
NB++;
int C[R + S];
}
NC++; merge_arrays(A, R, B, S, C);
}
15
Searching
1. Linear Search:
• Compares the item of interest with each element of Array
one by one.
• Traverses the Array sequentially to locate the desired
item.
Linear Search Algorithm
• LINEAR (DATA, N, ITEM, LOC)
if (data[mid] == item) {
• if (index != -1) {
• return mid; // Element found, return its index
• printf("Element %d found at index %d\n",
• } else if (item < data[mid]) {
item, index);
• end = mid - 1; // Search in the left half
• } else {
• } else {
Limitations of Binary Search
• Although the complexity of Binary Search is
O (log n), it has some limitations:
1. the list must be sorted
2. one must have direct access to the middle element in any
sublist.
Selection Sort
Selection Sort
Selection Sort
// Selection sort in C
// function to print an array
// function to swap the the position of two elements printf("%d ", array[i]);
printf("\n");
int temp = *a;
}
*a = *b;
*b = temp;
// driver code
}
int main() {
void selectionSort(int array[], int size) { int data[] = {20, 12, 10, 15, 2};
for (int step = 0; step < size - 1; step++) { int size = sizeof(data) / sizeof(data[0]);
for (int step = 1; step < size; step++) { printf("%d ", array[i]);
int j = step - 1;
printf("\n");
// Compare key with each element on the left of it until an
}
element smaller than
// it is found.