Lecture 2
Lecture 2
Algorithm
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
Insertion
Algorithm
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
Deletion
#include <stdio.h>
Output:
void main(){ The original array elements are :
int LA[] = {1,3,5}; LA[0] = 1
int n = 3; LA[1] = 3
int i; LA[2] = 5
printf("The original array elements are :\n"); The array elements after deletion :
for(i = 0; i<n; i++) LA[0] = 1
LA[1] = 5
printf("LA[%d] = %d \n", i, LA[i]);
for(i = 1; i<n; i++) {
LA[i] = LA[i+1];
n = n – 1;
}
printf("The array elements after deletion :\n");
for(i = 0; i<n-1; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
Search
Algorithm
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Search
#include <stdio.h>
Output:
void main(){ The original array elements are :
int LA[] = {1,3,5,7,8}; LA[0] = 1
int item = 5, n = 5; LA[1] = 3
int i = 0, j = 0; LA[2] = 5
printf("The original array elements are :\n"); LA[3] = 7
for(i = 0; i<n; i++) { LA[4] = 8
Found element 5 at position 3
printf("LA[%d] = %d \n", i, LA[i]);
}
for(i = 0; i<n; i++) {
if( LA[i] == item ) {
printf("Found element %d at position %d\n",
item, i+1);
}
}
}
Traversal
Algorithm
1. Start
2. Initialize an Array of certain size and datatype.
3. Initialize another variable ‘i’ with 0.
4. Print the ith value in the array and increment i.
5. Repeat Step 4 until the end of the array is reached.
6. End
Traversal
Algorithm
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Update
Algorithm
1. Start
2. Print all the elements in the Array
3. Stop
Display