Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Data Structures

& Algorithms
What is Data Structure
◼ Data Structure is a logical relationship
existing between individual elements of data
◼ the logical or mathematical model of a
particular organization of data is called "Data
Structure".
◼ A means of storing collection of data.
◼ It is the specification of the elements of the
structure, the relationship between them, and
the operations that may be performed on
them.
Data Structure Continued. . .
Data may be organized in many different ways; The
choice of a particular data model depends on two
considerations:
1. It must be rich enough in structure to reflect the
actual relationships of the data in the real world.
2. The structure should be simple enough that one can
effectively process the data when necessary.

Algorithm + Data structure → Program


Classification of Data Structure

Data Structure

Primitive Data Non primitive


Structure data structure

integer Float Character Pointer Array Lists Files

Linear Non linear


List List

Stacks Graphs

Queues Trees
Classification of Data Structure
Primitive Data Structure:
These are Basic structures and are directly
operated upon by machine instuructions,
Have different representation on different
computers.
Examples :
Real, Float, integer, character, pointer etc.
Classification of Data Structure
Non primitive Data Structures:
These are more sophisticated data
structures. they are derived from the primitive
data structure. The non primitive data
structure emphasize on structuring of a group
of homogeneous or heterogeneous data
items.
Examples:
Arrays, List and Files.
Operations on Data Structures
The particular data structure that one chooses for a
given situation depends largely on the nature of
specific operations to be performed.
The following are the four major operations associated
with any data structure:
1. Traversing
2. Searching
3. Insertion
4. Deletion
5. Sorting
6. Merging
Operations on Data Structure
◼ Traversing : Accessing each record exactly once
so that certain items in the record may be
processed.
◼ Searching : Finding the location of the record with
a given key value, or finding the locations of all
records which satisfy one or more conditions.
◼ Inserting : Adding a new record to the structure.
◼ Deleting : Removing a record from the structure.
◼ Sorting : Arranging the record in some logical
order.
◼ Merging : Combining the records in two sorted
files into a single file.
Abstract Data Type
◼ ADT is a mathematically specified entity
that defines a set of its instances, with:
◼A specific interface– a collection of
signatures of operations, that can be
invoked on an instance.
◼A set of axioms (preconditions &
Postconditions) that defines the semantics
of operation(i.e what the operations do to
instances of ADT, not how)
Abstract Data Types
◼ Types of Operations:
◼ Constructors
◼ Manipulation Procedures
◼ Access Functions
Abstract Data Type
◼ Why do we need to talk about ADT in data
structure???
◼ They serve as specifications of requirements for
the building blocks of solution to algorithmic
problems.
◼ Provides a language to talk of on a higher level of
abstraction.
◼ ADT’s encapsulate data structures and Algorithms
that implement them.
◼ Separates the issues of correctness and
efficiency.
Memory Allocation in ‘C’
There are two types of memory
allocation possible in C language
1. Compile time/static
2. Run-time/Dynamic
Memory Allocation in ‘C’
‘C’ provides four dynamic allocation
and de-allocation functions:
1. Malloc()
2. Calloc()
3. Free()
4. Realloc()
Memory Allocation in ‘C’
◼ Malloc()
Eg→ int ptr;
Ptr=(int *)malloc(10*sizeof(int))
◼ Calloc()
eg→ int *ptr;
ptr=(int *)calloc(10,2);
◼ Free()
Eg→ free(ptr)
◼ Realloc()
Eg→ ptr=realloc(ptr,new_size)
Example to show use of Malloc()
Void main()
{
int *ptr, I,n;
printf(“enter no of element u want to store in array”);
Scanf(“%d”,&n)
ptr=(int *)malloc(sizeof(int)*n);
printf(“\n Enter the elements”);
For(i=0;i<n;i++)
scanf(“%d”,ptr+i);
printf(“u have entered\n”);
for(i=0;i<n;i++)
printf(“%d \t”,*(ptr+i));
getch();
}
Binary Search
◼ The binary search algorithm is naturally
recursive.
◼ That is, the action that we perform on the
original list is the very same as the action that
we perform on the sublists.
◼ As a consequence, it is very easy to write a
recursive binary search function.
Binary Search
◼ Binary search uses a recursive method to
search an array to find a specified value
◼ The array must be a sorted array:
a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]
◼ If the value is found, its index is returned
◼ If the value is not found, -1 is returned
◼ Note: Each execution of the recursive
method reduces the search space by about a
half
Binary Search
◼ An algorithm to solve this task looks at
the middle of the array or array segment
first
◼ If the value looked for is smaller than
the value in the middle of the array
◼ Then the second half of the array or array
segment can be ignored
◼ This strategy is then applied to the first half
of the array or array segment
Binary Search
◼ If the value looked for is larger than the value
in the middle of the array or array segment
◼ Then the first half of the array or array segment
can be ignored
◼ This strategy is then applied to the second half of
the array or array segment
◼ If the value looked for is at the middle of the
array or array segment, then it has been
found
◼ If the entire array (or array segment) has
been searched in this way without finding the
value, then it is not in the array
Binary search Algo
Algo: Binary(a,lb,ub,iem,loc)
//a is a sorted array with lowerbound lb and upper bound ub.
// Item is value need to be searched
1. [initialize variables]
Set beg=lb,end=ub-1, mid=beg+end)/2
2. Repeat 3 and 4 while(beg<=end) and a[mid]!=item.
3. If(item<a[mid])
Set end=mid-1;
Else
Set beg-mid+1
4. Set mid=int(beg+end)/2
5. if(a[mid]==item)
Set loc=mid
Else loc=null
6. Exit
Iterative Binary Search
#include<stdio.h> while((beg<=end)&&(a[mid]!=item))
#include<conio.h> {
mid=(beg+end)/2;
void main()
if (item==a[mid])
{
{ loc=mid;
int a[10],item,beg,end; f=1;
Int n,loc,mid,i,f=0; printf("\n element found at position
clrscr(); %d",loc+1);
printf("enter the array size"); break;
}
scanf("%d",&n);
else if(item< a[mid])
printf("\n enter array elements");
end=mid-1;
for(i=0;i<n;i++) else
scanf("%d",&a[i]); beg=mid+1;
printf("\n eneter item to be
searched"); }
scanf("%d",&item); if(f==0)
loc=0;beg=0;end=n-1; printf("element not found");
getch();
}

Execution of Binary search
Execution of Binary search
BinarySearch(int a[], int value,
int left, int right)
◼ {
◼ // See if the search has failed
◼ if (left > right)
◼ return –1;
◼ // See if the value is in the first half
◼ int middle = (left + right)/2;
◼ if (value < a[middle])
◼ return BinarySearch(a, value, left, middle –
1);
◼ // See if the value is in the second half
◼ else if (value > a[middle])
◼ return BinarySearch(a, value, middle + 1,
right);
◼ // The value has been found
◼ elsemiddle;
◼ }
Recursive Binary Search In C
int bsearch(int a[ ], Int lb, I nt ub, int item)
{
int mid;
if(lb>ub)
return(-1);
mid=(int) (lb+ub)/2;
if(a[mid]==item)
return(mid);
if(a[mid]>item)
bsearch(a,lb,mid-1,item);
else
bsearch(a,mid+1,ub,item);
return;
}
Binary Search in C
void main() printf("\nenter item to be
{ searched");
int n,i,item,loc; scanf("%d",&item);
int a[10]; loc= bsearch(a,0,n-
printf("enter the size of 1,item);
array"); if( loc<0)
scanf("%d",&n); printf("element not found");
printf("\n enter elements"); else printf("element found
for(i=0;i<n;i++) at location %d",loc);
scanf("%d",&a[i]); getch();
}
Array Representation in Memory

1. Row Major Representation


2. Column Major Representation
Composites: Arrays
◼ Implementation: 2D array
(1) Storage Layout var A : array[L1..U1] of [L2..U2] of T
(a) ROW-Major Layout

[L2] [L2+1] [L2+2] ... [U2-1] [U2]


A[L1]

A[L1+1]
...

A[U1]

... ...
Memory is a contiguous block
28
Base address @
Composites: Arrays
◼ Implementation: 2D array
(1) Storage Layout
(b) COLUMN-Major Layout

var A : array[L1..U1] of [L2..U2] of T


[L2] [L2+1] [L2+2] ... [U2-1] [U2]
A[L1]

A[L1+1]
...

A[U1]

... ...
Memory is a contiguous block
29
Base address @
Row Major representation
◼ Address of A[i][j]= B + W(n(i-l1)+(j-l2))
◼ Where
◼ B→ base address
◼ W→ size of each array element
◼ N→ number of columns(U2-L2+1)
◼ Implementation: 2D array
(2) Operation: Array subscripting. Eg. ROW-major Layout

var A : array[L1..U1] of [L2..U2] of T


[L2] [L2+1] [L2+2] ... [U2-1] [U2]
A[L1]

A[L1+1]
...

A[U1]

... ...
Memory is a contiguous block
Base address Skip # of rows row size Skip # of cells
@
L-value of A[i,j] = @ + (i-L1)*(U2-L2+1)*cellsize + (j-L2)*cellsize

“Go to the correct row” “Go to the correct


31

cell”
Example:→Given Array Int a[4. .7,-1..3]. Calculate the
address of element at location a[6][[2]. Given that base
address is 100.

◼ Base address B=100


◼ Size of each array element n=2
◼ Lower bound of row L1=4
◼ Lower bound of column L2=-1
◼ Upper bound of row U1=7
◼ Upper bound of row U2=3
◼ i=6, j=2

◼ N=u2-l2+1
◼ Address of A[i][j]= B + W(n(i-L1)+(j-L2))

◼ ANS=?????
Queries

You might also like