CS443- Data Structures & Algorithms
Lecture # 3 – Array
Operations
OBJECTIVES
• Today’s lecture objectives include:
• Arrays
• Operation on Arrays
• Traversal
• Searching (Binary and Sequential)
• Insertion
• Updation
• Deletion
TRAVERSAL
• Traverse−to print all the array elements one by one.
• Or to process each element one by one.
• Let array A be a collection of data elements stored in the memory of
the computer. Suppose we want to print the content of each element
of A or suppose we want to count the number of elements of A with
given property. This can be accomplished by traversing A, that is, by
accessing and processing (frequently called visiting) each element of
A exactly once.
ALGORITHM
• Step 1 :[Initialization] Set i = LB
• Step 2 : Repeat Step 3 and Step 4 while i < = UB
• step 3 : [ processing ] Process the A[i] element
• Step 4 : [ Increment the counter ] i = i + 1
[ End of the loop of step 2 ]
• Step 5: Exit
(Here LB is lower Bound and UB is Upper Bound A[ ] is linear array )
CODE
#include <iostream.h>
A [5]= {1,3,5,7,9};
Int i=0, u=4;
while ( I <= u)
{
cout<< A[i]<<endl;
i++;
}
SEARCHING (LINEAR)
• This method of searching is also known as sequential search.
• Linear search work on both sorted as well as unsorted list.
• This method of searching works as follows:
• Suppose we want to search an element X from the list of N elements. First the
element X is compared with the 1st
element in the List. If the element is
matched then search is successful. If the element is not matched then the
element X is compared with next element and this process is repeated until
match is found or all the elements in the list are compared.
ALGORITHM
Consider LA is a linear array with N elements and K is a positive integer such
that K<=N. Following is the algorithm to find an element with a value of ITEM
using sequential search.
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
CODE
#include <stdio.h>
Void main(){
Int LA[]={1,3,5,7,8};
Int item =5,n =5, j =0;
while(j <n) {
if(LA[j]==item )
break;
j =j +1; } //while loop end
cout <<"Found element” <<item<<“at position ”<<j+1);
}
UPDATING ARRAY
• Update operation refers to updating an existing element from the
array at a given index.
• Algorithm
Consider LA is a linear array with N elements and K is a positive integer
such that K<=N. Following is the algorithm to update an element
available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. End
CODE
#include<stdio.h>
Void main()
{
int LA[]={1,3,10,5,7,8};
int item =10, k=3;
LA[k-1]==item;
}
INSERTION
• Insert operation is to insert one or more data elements into an array.
• Based on the requirement, a new element can be added at the
beginning, end, or any given index of array.
ALGORITHM
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such
that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
CODE
#include<stdio.h>
main(){
int LA[]={1,3,5,7,8};
Int item =10,k =3,n =5;inti=0,j =n;
n =n +1;
while(j >=k){
LA[j+1]=LA[j];
j =j -1;}
LA[k]=item;
}
DELETION
• Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
ALGORITHM
• Consider LA is a linear array with N elements and K is a positive integer such
that K<=N. Following is the algorithm to delete an element available at the
Kth position of LA.
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
CODE
#include<stdio.h>
Void main(){
int LA[]={1,3,5,7,8};
int k =3,n =5;
int i, j ;
j =k;
while (j <= n) {
LA [j-1] = LA[j];
j =j +1;}
n =n -1;}

Lecture 3 - Array Operations in C++ including all concepts of arrays in data structure and algorithms

  • 1.
    CS443- Data Structures& Algorithms Lecture # 3 – Array Operations
  • 2.
    OBJECTIVES • Today’s lectureobjectives include: • Arrays • Operation on Arrays • Traversal • Searching (Binary and Sequential) • Insertion • Updation • Deletion
  • 3.
    TRAVERSAL • Traverse−to printall the array elements one by one. • Or to process each element one by one. • Let array A be a collection of data elements stored in the memory of the computer. Suppose we want to print the content of each element of A or suppose we want to count the number of elements of A with given property. This can be accomplished by traversing A, that is, by accessing and processing (frequently called visiting) each element of A exactly once.
  • 4.
    ALGORITHM • Step 1:[Initialization] Set i = LB • Step 2 : Repeat Step 3 and Step 4 while i < = UB • step 3 : [ processing ] Process the A[i] element • Step 4 : [ Increment the counter ] i = i + 1 [ End of the loop of step 2 ] • Step 5: Exit (Here LB is lower Bound and UB is Upper Bound A[ ] is linear array )
  • 5.
    CODE #include <iostream.h> A [5]={1,3,5,7,9}; Int i=0, u=4; while ( I <= u) { cout<< A[i]<<endl; i++; }
  • 6.
    SEARCHING (LINEAR) • Thismethod of searching is also known as sequential search. • Linear search work on both sorted as well as unsorted list. • This method of searching works as follows: • Suppose we want to search an element X from the list of N elements. First the element X is compared with the 1st element in the List. If the element is matched then search is successful. If the element is not matched then the element X is compared with next element and this process is repeated until match is found or all the elements in the list are compared.
  • 7.
    ALGORITHM Consider LA isa linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search. 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
  • 8.
    CODE #include <stdio.h> Void main(){ IntLA[]={1,3,5,7,8}; Int item =5,n =5, j =0; while(j <n) { if(LA[j]==item ) break; j =j +1; } //while loop end cout <<"Found element” <<item<<“at position ”<<j+1); }
  • 9.
    UPDATING ARRAY • Updateoperation refers to updating an existing element from the array at a given index. • Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA. 1. Start 2. Set LA[K-1] = ITEM 3. End
  • 10.
  • 11.
    INSERTION • Insert operationis to insert one or more data elements into an array. • Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
  • 12.
    ALGORITHM Let LA bea Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA 1. Start 2. Set J = N 3. Set N = N+1 4. Repeat steps 5 and 6 while J >= K 5. Set LA[J+1] = LA[J] 6. Set J = J-1 7. Set LA[K] = ITEM 8. Stop
  • 13.
    CODE #include<stdio.h> main(){ int LA[]={1,3,5,7,8}; Int item=10,k =3,n =5;inti=0,j =n; n =n +1; while(j >=k){ LA[j+1]=LA[j]; j =j -1;} LA[k]=item; }
  • 14.
    DELETION • Deletion refersto removing an existing element from the array and re- organizing all elements of an array.
  • 15.
    ALGORITHM • Consider LAis a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA. 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
  • 16.
    CODE #include<stdio.h> Void main(){ int LA[]={1,3,5,7,8}; intk =3,n =5; int i, j ; j =k; while (j <= n) { LA [j-1] = LA[j]; j =j +1;} n =n -1;}