Data Structures (Chapter 03 Array)
Data Structures (Chapter 03 Array)
STRUCTURES
WITH COMPETITIVE
CODING
Presented By: Mr. Satyananda Swain
Assistant Professor, Computer Science Engineering,
▪ Index − Each location of an element in an array has a numerical index, which is used to identify the element.
❑ Array Representation:
❑ Address Calculation:
LOC(LA[K])= Base(LA) + w(K-LB)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Operations on Linear Array 2
❑ Basic Operations
▪ Sorting – Arrange the elements of the array in ascending, descending or alphabetical order.
int i;
scanf("%d",n);
for(i=0;i<*n;i++)
scanf("%d",&a[i]);
int i;
printf("%d ",a[i]);
▪ ITEM is the element to be inserted in LA. Step 6: Set I : = I-1. [End of loop of Step-4]
▪ POS is a positive integer such that POS <= N. (Position of Step 7: Set LA[POS] : = ITEM.
This algorithm is used to insert ITEM into the POS position Step 9: Stop.
of LA.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Insertion Operation: Pseudocode 8
int i, item=a[pos-1];
for(i=pos-1;i<*n-1;i++)
a[i]=a[i+1];
(*n)--;
traverse(a,0,*n-1);
}
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Searching Operation 11
❑ Linear Search: The linear search algorithm is the
Step 1. Start.
simplest algorithm to do sequential search and this
Step 2. Let I.
technique iterates over the sequence and checks one
item at a time until the desired item is found or all Step 3. Repeat for I:=1 to N incremented by 1, then:
items have been examined. One can perform a Step 4. If (LA[I] = ITEM), then:
search for an array element based on its value or its Step 4.1) Print “Successful Search At Position =”, I
index. and Break. [End of if of Step-4]
❑ Algorithm: LINEAR_SEARCH(LA, N, ITEM) [End of loop of Step 3]
Step 6. Stop.
This algorithm is used to find the position of ITEM in LA.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Linear Search: Pseudocode 12
▪ If the middle item is the target value, then the search item is found and it returns True.
▪ If the target item < middle item, then search for the target value in the first half of the list.
▪ If the target item > middle item, then search for the target value in the second half of the list.
❑ In binary search as the list is ordered, so we can eliminate half of the values in the list in each iteration.
Step 2. Let BEG, END, MID and LOC. Step 8. If (LA[MID] = ITEM), then:
Step 3: Set BEG : = LB, END : = UB and MID : = INT((BEG + END)/2). Step 8.1) Set LOC : = MID.
Step 4. Repeat step 4 to 7 while(BEG <= END and LA[MID] != ITEM) , then: Step 8.2) Print “Successful Search at ”, LOC.
Step 5.1) Set END : = MID – 1. Step 9.1) Set LOC : = NULL.
Step 1: Start.
❑ Algorithm: BUBBLE_SORT(LA, N) Step 2: Let I, J.
▪ LA is a linear array with elements. Step 3: Repeat For I : = 1 to N-1 incremented by 1, then:
▪ N is the no. of elements in LA. Step 3.1: Repeat for J : = 1 to N - I incremented by 1, then:
▪ ITEM is the element to be searched. Step 3.1.1) If (LA[J] > LA[J+1]), then:
This algorithm is used to Sort the elements of LA in Step 3.1.1.1) Interchange LA[J] and LA[J+1].
Step 4: Exit.
❑ C-CODE scanf("%d",&a[i]);
#include<stdio.h> }
{ {
int i; int i;
scanf("%d",n); for(i=lb;i<=ub;i++)
for(i=0;i<*n;i++) }
int i=*n-1; {
{ for(i=pos-1;i<*n;i++)
a[i+1]=a[i]; a[i]=a[i+1];
i--; (*n)--;
traverse(a,0,*n-1);
{ for(i=0;i<n-1;i++)
int i; {
for(i=0;i<n;i++) for(j=0;j<(n-i)-1;j++)
if(a[i]==s) {
{ if(a[j]>a[j+1])
break; t=a[j];
} a[j]=a[j+1];
if(i==n) a[j+1]=t;
} traverse(a,0,n-1);
printf("\nMENU:\n1->Display\n2->Insetr\n3->Delete\n4- scanf("%d",&s);
{ }
case 2: system("PAUSE");
scanf("%d%d",&item,&pos); }
insert(a,&n,item,pos);break;