2. Assign J = N
3. Assign N = N + 1
4. Repeat steps 5 and 6 when J> = K
5. Assign LA [J + 1] = LA [J]
6. Assign J = J-1
7. Assign LA [K] = ITEM
8. Finish
The following is the full code of the above algorithm in C language:
#include main () { int LA [] = { 1 , 3 , 5 , 7 , 8 }; int item = 10 , k = 3 , n = 5 ; int i = 0 , j = n ; printf ( "Danh sach phan tu trong mang ban dau:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } n = n + 1 ; while ( j >= k ){ LA [ j + 1 ] = LA [ j ]; j = j - 1 ; } LA [ k ] = item ; printf ( "Danh sach phan tu cua mang sau hoat dong chen:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } }
Compiling and running the above C program will result:
The delete operation is to delete an existing element from an array and reorganize the remaining elements in that array.
For example
Suppose LA is a linear array with N elements and K is a positive integer that satisfies K <= N. Here is the algorithm to delete an element in the LA array at position K.
Algorithms
1. Start
2. Assign J = K
3. Repeat steps 4 and 5 while J4. Assign LA [J-1] = LA [J]
5. Assign J = J + 1
6. Assign N = N-1
7. Finish
The following is the full code of the above algorithm in C language:
#include main () { int LA [] = { 1 , 3 , 5 , 7 , 8 }; int k = 3 , n = 5 ; int i , j ; printf ( "Danh sach phan tu trong mang ban dau:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } j = k ; while ( j < n ){ LA [ j - 1 ] = LA [ j ]; j = j + 1 ; } n = n - 1 ; printf ( "Danh sach phan tu trong mang sau hoat dong xoa:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } }
Compiling and running the above C program will result:
You can perform the search for the element in the array based on the element's value or index.
For example
Suppose LA is a linear array with N elements and K is a positive integer that satisfies K <= N. Here is the algorithm to find an ITEM element using sequential search method (or search for route). calculation).
Algorithms
1. Start
2. Assign J = 0
3. Repeat steps 4 and 5 when J4. If LA [J] is equal to ITEM TO STEP 6
5. Assign J = J +1
6. In value J, ITEM
7. Finish
The following is the full code of the above algorithm in C language:
#include main () { int LA [] = { 1 , 3 , 5 , 7 , 8 }; int item = 5 , n = 5 ; int i = 0 , j = 0 ; printf ( "Danh sach phan tu trong mang ban dau:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } while ( j < n ){ if ( LA [ j ] == item ){ break ; } j = j + 1 ; } printf ( "Tim thay phan tu %d tai vi tri %dn" , item , j + 1 ); }
Compiling and running the above C program will result:
The update operation is to update the value of an existing element in the array at the given index.
Algorithms
Suppose LA is a linear array with N elements and K is a positive integer that satisfies K <= N. Here is an algorithm to update the element value at position K of the LA array.
1. Start
2. Set up LA [K-1] = ITEM
3. Finish
The following is the full code of the above algorithm in C language:
#include main () { int LA [] = { 1 , 3 , 5 , 7 , 8 }; int k = 3 , n = 5 , item = 10 ; int i , j ; printf ( "Danh sach phan tu trong mang ban dau:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } LA [ k - 1 ] = item ; printf ( "Danh sach phan tu trong mang sau hoat dong update:n" ); for ( i = 0 ; i < n ; i ++) { printf ( "LA[%d] = %d n" , i , LA [ i ]); } }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous article: Setting environment in Data structure
Next lesson: What is algorithm?