0% found this document useful (0 votes)
2 views

Data Structures (Chapter 03 Array)

The document provides an overview of data structures, specifically focusing on linear arrays, their operations, and algorithms for creating, traversing, inserting, deleting, searching, and sorting elements. It includes algorithms and C implementations for each operation, such as linear search and bubble sort. The content is presented by Mr. Satyananda Swain from Centurion University, Bhubaneswar, Odisha.

Uploaded by

rout93596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structures (Chapter 03 Array)

The document provides an overview of data structures, specifically focusing on linear arrays, their operations, and algorithms for creating, traversing, inserting, deleting, searching, and sorting elements. It includes algorithms and C implementations for each operation, such as linear search and bubble sort. The content is presented by Mr. Satyananda Swain from Centurion University, Bhubaneswar, Odisha.

Uploaded by

rout93596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

DATA

STRUCTURES
WITH COMPETITIVE
CODING
Presented By: Mr. Satyananda Swain
Assistant Professor, Computer Science Engineering,

SCHOOL OF ENGINEERING AND TECHNOLOGY,


CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
DATA STRUCTURES
Chapter-3: Array
Presented By: Mr. Satyananda Swain
(Assistant Professor)
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY,
BHUBANESWAR, ODISHA
Introduction: Linear Array 1
❑ Array is a container that can hold a fixed number of items, which should be the same type. Most of the data structures
make use of arrays to implement their algorithms. Following are the important terms to understand the concept of
Array.

▪ Element − Each item stored in an array is called an element.

▪ 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

▪ Creation- Create and initialize elements in an array.

▪ Traverse − Print all the array elements one by one.

▪ Insertion − Adds an element at the given index.

▪ Deletion − Deletes an element at the given index.

▪ Search − Searches an element using the given index or by the value.

▪ Sorting – Arrange the elements of the array in ascending, descending or alphabetical order.

▪ Merging – Join two or more arrays into one unit.

▪ Update − Updates an element at the given index.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Create Operation: Algorithm 3

❑ Create operation deals with the


Step 1: Start.
initialization of elements in an array.
Step 2: Let I.
❑ Algorithm: CREATE_LINEAR_ARRAY(LA, N)
Step 3: Print “Enter How many elements(N):”
▪ Here, LA is a linear array with N
Step 4: Read N.
elements.
Step 5: Repeat for I : = 1 to N incremented by 1, then:
▪ N is the no. of elements in LA. Step 5.1) Read LA[I].

This algorithm is used to initialize N [End of loop of step 5]


elements in LA. Step 6: Stop.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Create Operation: Pseudocode 4

❑ Following is the C implementation of the Create algorithm

void create(int a[],int *n)

int i;

printf("\n Enter how many elements:\n");

scanf("%d",n);

printf("\nEnter data into array:\n");

for(i=0;i<*n;i++)

scanf("%d",&a[i]);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Traverse Operation: Algorithm 5

❑ Traversal refers to visiting or printing all elements


from the array exactly once from the least index
Step 1: Start.
position to the highest index position of an array.
Step 2: Let I.
❑ Algorithm: TRAVERSE_LINEAR_ARRAY(LA, LB, UB)
Step 3: Set I : = LB.
▪ Here, LA is a linear array with N elements.
Step 4: Repeat steps 5 and 6 while (I <= UB), then:
▪ LB and UB are the lower bound and upper Step 5: Apply Process or Print LA[I].
bound.
Step 6: Set I : = I + 1. [End of loop of step 3]
This algorithm is used to traverse all elements Step 7: Stop.
available in LA.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Traverse Operation: Pseudocode 6

❑ Following is the C implementation of the Traversal algorithm

void traverse (int a[ ], int lb, int ub)

int i;

printf("\nContent of array is:\n");

for(i=lb; i<=ub; i++)

printf("%d ",a[i]);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Insertion Operation: Algorithm 7

❑ Insert operation inserts one or more data elements into


an array. Based on the requirement, a new element can Step 1: Start.
be added at the array's beginning, end, or any given Step 2: Let I.
index.
Step 3: Set I : = N.
❑ Algorithm: INSERTION_LINEAR_ARRAY(LA, N, ITEM, POS) Step 4: Repeat steps 5 and 6 while (I >= POS), then:
▪ Here, LA is a Linear Array (unordered) with N elements. Step 5: Set LA[I+1] : = LA[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.

insertion). Step 8: Set N : = N + 1.

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

❑ Following is the C implementation of the insertion algorithm.


void insert (int a[], int *n, int item, int pos)
{
int i=*n-1;
while(i>=pos-1)
{
a[i+1]=a[i];
i--;
}
a[pos-1]= item;
(*n)++;
printf("\nAfter Insertion array is:\n");
traverse(a,0,*n-1);
}

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
9
Deletion Operation: Algorithm
❑ Deletion refers to removing an existing element
Step 1: Start.
from the array and re-organizing all elements of
Step 2: Let I, ITEM.
an array.
Step 3: Set ITEM : = LA[POS].
❑ Algorithm: DELETION_LINEAR_ARRAY(LA, N, POS)
Step 4: Repeat for I : = POS to N-1 incremented by 1, then:
▪ LA is a linear array with N elements
Step 4.1) Set LA[I] : = LA[I + 1].
▪ POS is a positive integer such that POS <= N.
[End of Loop of Step-4]
(Position of deletion)
Step 5: Set N : = N-1.
This algorithm is used to delete an element available
Step 6: Print “Item Deleted=”, ITEM.
at the POS position of LA.
Step 7: Stop

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Deletion Operation: Pseudocode 10

❑ Following is the C implementation of the deletion algorithm

void delete (int a[], int *n, int pos)

int i, item=a[pos-1];

for(i=pos-1;i<*n-1;i++)

a[i]=a[i+1];

(*n)--;

printf("\nItem deleted:%d\nAfter Deletion array is:\n",item);

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]

▪ LA is a linear array with N elements. Step 5. If (I = N+1), then:

▪ N is the no. of elements in LA. Step 5.1) Print “Unsuccessful Search”.

▪ ITEM is the element to be searched. [End of if of Step-4]

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

❑ Following is the C implementation of the linear search algorithm


void linear_search(int a[10], int n, int s)
{
int i;
for(i=0;i<n;i++)
if(a[i]==s)
{
printf("\n%d found at %d position",s,i+1);
break;
}
if(i==n)
printf("\n %d not found",s);
}

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Searching Operation 13
❑ Binary Search: In Binary search algorithm, the target key is examined in a sorted sequence and this algorithm starts
searching with the middle item of the sorted sequence.

▪ 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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Binary Search: Algorithm 14
❑ Algorithm: BINARY_SEARCH(LA, LB, UB, ITEM)
▪ LA is a linear array with N elements
▪ LB and UB are the lower bound and upper bound.
▪ ITEM is the element to be searched.
This algorithm is used to find the position of ITEM in LA using Binary Search mechanism.
Step 1. Start. [End of loop of Step 4]

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. If (ITEM < LA[MID]), then: Step 9: Else, then:

Step 5.1) Set END : = MID – 1. Step 9.1) Set LOC : = NULL.

Step 6: Else, then: Step 9.2) Print “Unsuccessful Search ”, LOC.

Step 6.1) Set BEG : = MID + 1. [End of if of Step-5] [End of if of Step-8]

Step 7: Set MID : = INT((BEG + END)/2). Step 10. Stop.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Binary Search: Pseudocode 15
❑ Following is the C implementation of the Binary search }
algorithm else
void binary_search(int a[10], int lb, int ub,int item) printf("\nUnsuccessful Search\n");
{ }
int beg=lb, end=ub, mid=(beg + end)/2, pos;
while(beg<=end && a[mid]!=item)
{
if(item<a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
pos=mid+1;
printf("\nSuccesful Search\n%d found at %d
pos.\n", item, pos);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Sorting Operation 16
❑ Bubble Sort: This sorting technique is also known as
exchange sort, which arranges values by iterating over
the list several times and in each iteration the larger
value gets bubble up to the end of the list.

❑ This algorithm uses multiple passes and, in each pass,


the first and second data items are compared. if the first
data item is bigger than the second, then the two items
are swapped. Next the items in second and third position
are compared and if the first one is larger than the
second, then they are swapped, otherwise no change in
their order. This process continues for each successive
pair of data items until all items are sorted.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Bubble Sort: Algorithm 17

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].

ascending order using Bubble Sort mechanism. [End of if of Step 3.1.1]

[End of Inner Loop of step-3.1 ]

[End of Outer Loop of step-3]

Step 4: Exit.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Bubble Sort: Pseudocode 18

Following is the C implementation of the if(a[j]>a[j+1])


Bubble Sort algorithm. {
void bubble_sort(int a[],int n)
t=a[j];
{ a[j]=a[j+1];
int i,j,t; a[j+1]=t;
for(i=0;i<n-1;i++)
} } }
{ }
for(j=0;j<(n-i)-1;j++)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
19
Array Operations: Complete C Code
❑ Write a C program that uses functions to perform the following operations
on an array.
a. Creation.
b. Insertion.
c. Deletion.
d. Traversal.
e. Searching.
f. Sorting.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Array Operations: Complete C Code 20

❑ C-CODE scanf("%d",&a[i]);

#include<stdio.h> }

void create(int a[],int *n) void traverse(int a[],int lb,int ub)

{ {

int i; int i;

printf("\n Enter how many elements:\n"); printf("\nContent of array is:\n");

scanf("%d",n); for(i=lb;i<=ub;i++)

printf("\nEnter data into array:\n"); printf("%d ",a[i]);

for(i=0;i<*n;i++) }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Array Operations: Complete C Code 21
void insert(int a[],int *n,int item,int pos) }

{ void delete(int a[],int *n,int pos)

int i=*n-1; {

while(i>=pos-1) int i,item=a[pos-1];

{ for(i=pos-1;i<*n;i++)

a[i+1]=a[i]; a[i]=a[i+1];

i--; (*n)--;

} printf("\nItem deleted:%d\nAfter Deletion array


is:\n",item);
a[pos-1]= item;
traverse(a,0,*n-1);
(*n)++;
}
printf("\nAfter Insertion array is:\n");

traverse(a,0,*n-1);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Array Operations: Complete C Code 22
void linear_search(int a[10],int n,int s) int i,j,t;

{ 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])

printf("\n%d found at %d position",s,i+1); {

break; t=a[j];

} a[j]=a[j+1];

if(i==n) a[j+1]=t;

printf("\n %d not found",s); } } }

} traverse(a,0,n-1);

void bubble_sort (int a[],int n) }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Array Operations: Complete C Code 23
int main() case 3:
{ printf("Enter the postion for deletion:\n");
int a[100],op,n,pos,item,s; scanf("%d",&pos);
create(a,&n); delete(a,&n,pos);break;
do case 4:
{ printf("Enter the element to be searched:\n");

printf("\nMENU:\n1->Display\n2->Insetr\n3->Delete\n4- scanf("%d",&s);

>Search\n5->Sort\n6->Exit\nEnter your choice:\n\n"); linear_search(a,n,s);break;

scanf("%d",&op); case 5: bubble_sort (a,n);break;

switch(op) case 6: exit(0);

{ }

case 1:traverse(a,0,n-1);break; } while(op<7);

case 2: system("PAUSE");

printf("Enter the item and postion of insertion:\n"); return 0;

scanf("%d%d",&item,&pos); }

insert(a,&n,item,pos);break;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA

You might also like