Professional Documents
Culture Documents
For data processing applications, large amount of data needs to be operated upon.
Typical operations on these data from data processing viewpoint are:
1. Insert a new value/ record
2. Delete and existing value/ record
3. Search for a value/ record
4. Sort all values/ records
5. Traverse all values/ records
We need some ways to organize these data so that all above operations can be performed
efficiently. This organisation of data is studied in Data Structures.
For example: consider arrays having sorted values in ascending order.
Insert operation: Algorithm:
1. Search for the required location.
2. Pull remaining values down by one place to make room for new element.
3. Place the value there.
Seems good. Consider population of India stored in an array. To insert a new element 130
crore values to be searched and remaining values to be pulled down to create space for new
element. Complexity O(N). Agreed. Also to pull down remaining values, code as follows
required: (no. of elements=n; new element inserted at position: x)
for(i=n-1; i>=x; i--) a[i+1]=a[i];
DATA STRUCTURES: INTRODUCTION
Delete operation: Same as Insert operation except now the remaining values are to be
pushed up to fill the space created by deleted element. Complexity O(N). Code:
for(i=x;i<n;i++) a[i]=a[i+1];
Search Operation: Three algorithms:
1. Linear Search: Compare with each value starting from beginning. Worst case it is at the
last position. Best case at the beginning. So
Best case complexity: Omega(1);
Worst case complexity: O(N);
Average case complexity: Theta(N/2) or Theta(N).
2. Binary Search: It will be discussed after this part.
3. Hashing: It will discussed later.
Sorting: To sort the given data in an array, we will study number of algorithms.
Non-recursive algorithms: Selection sort, Insertion sort, Bubble sort
Recursive algorithms: Quick sort, Merge sort, Heap sort.
Traversing: Scan each element one by one starting from the first.
Complexity: O(N), Theta(N), Omega(N).
CAN WE DO BETTER THAN THIS? YES. (THAT IS THE
PURPOSE OF THIS COURSE).
BASIC OPERATIONS ON ARRAYS:
#include<stdio.h>
#define N 20
void insertarray(int *a,int x,int *n)
{
int i;
i=*n;
while(i>0 && a[i-1]>x)
{
a[i]=a[i-1];
i--;
}
a[i]=x;
(*n)++;
}
void deletearray(int *a,int x,int *n)
{
int i=0,j;
while(i<*n && a[i]!=x)
i++;
if (i<*n)
{
for(j=i;j<*n-1;j++)
a[j]=a[j+1];
(*n)--;
}
}
BASIC OPERATIONS ON ARRAYS:
int linearsearch(int *a,int x,int n)
{
int i=0;
while(i<n && a[i]!=x)
i++;
if (i==n)
return -1;
else
return i;
}
void selectionsort(int *a,int n)
{
printf("will be implemented later\n");
}
void traversearray(int *a,int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
BASIC OPERATIONS ON ARRAYS:
int main()
{
int p[N]={1,3,5,7,9};
int no=5,choice,v;
do{
printf("1. Enter a value\n");
printf("2. Delete a value\n");
printf("3. Search a value\n");
printf("4. Sort values\n");
printf("5. traverse and print\n");
printf("6. EXIT\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Give value to enter: ");
scanf("%d",&v);
if (no<N)
insertarray(p,v,&no);
else
printf("array full, cannot enter\n");
break;
case 2:
printf("give value to delete: ");
scanf("%d",&v);
deletearray(p,v,&no);
break;
BASIC OPERATIONS ON ARRAYS:
case 3:
printf("give value to search: ");
scanf("%d",&v);
printf("value found at location: %d\n",linearsearch(p,v,no));
break;
case 4:
selectionsort(p,no);
printf("Sorting will be implemented later\n");
break;
case 5:
traversearray(p,no);
break;
case 6:
printf("Thank you, BYE\n");
break;
default:
printf("invalid choice, enter again\n");
}
} while(choice!=6);
}
BASIC OPERATIONS ON ARRAYS:
Linear Search:
1. For unsorted array:
int linearsearch(int *a,int x,int n) // n is the number of elements in the array
{ // index will range from 0 to n-1
int i=0;
while(i<n && a[i]!=x)
i++;
if (i==n)
return -1;
else
return i;
}
2. Sorted array: Can we improve it for Sorted array (increasing order):
int linearsearch(int *a,int x,int n)
{
int i=0;
while(i<n && x>a[i])
i++;
if (x==a[i]))
return i;
else
return -1;
}
BASIC OPERATIONS ON ARRAYS:
Linear Search: (A more general approach: starting index s and ending index e is given)
1. For unsorted array:
int linearsearch(int *a,int x,int s,int e) // number of elements in array = e-s+1
{ // s: starting index; e: ending index
int i=s;
while(i<=e && a[i]!=x)
i++;
if (i==e+1)
return -1;
else
return i;
}
2. Sorted array: Can we improve it for Sorted array (increasing order):
int linearsearch(int *a,int x,int s,int e)
{
int i=s;
while(i<=e && x>a[i])
i++;
if (x==a[i]))
return i;
else
return -1;
}
BASIC OPERATIONS ON ARRAYS:
Recursive Linear Search:(For unsorted array):
int rlinearsearch(int *a,int x,int s,int e)
{
If (a[s]==x)
return s;
else if (s>e)
return -1;
else
return rlinearsearch(a,x,s+1,e);
}
DELETE N N N
SEARCH:
LINEAR 1 N N
BINARY 1 log2N log2N
HASHING 1 N N
SORT (NON-RECURSIVE)
SELECTION N2 N2 N2
INSERTION N N2 N2
BUBBLE N N2 N2
TRAVERSE N N N
WHAT NEXT?
IMPLEMENTATION OF FOLLOWING DATA STRUCTURES USING ARRAYS:
(STATIC MEMORY ALLOCATION):
1. STACK AND ITS APPLICATIONS (POLISH NOTATIONS)
2. QUEUE - LINEAR AND CIRCULAR
3. LINKED LIST
{ {
while(!emptystack(topa) && while(!emptystack(topa) &&
peek(a,topa)!='(' && prio(peek(a,topa))>=prio(s[i])) peek(a,topa)!=')' && prio(peek(a,topa))>prio(s[i]))
{ pop(a,&c,&topa); { pop(a,&c,&topa);
printf("%c",c); push(b,c,&topb);
} }
push(a,s[i],&topa); push(a,s[i],&topa);
} }
i++; i--;
} }
while (!emptystack(topa)) while (!emptystack(topa))
{ {
pop(a,&c,&topa); pop(a,&c,&topa);
printf("%c",c); push(b,c,&topb);
} }
printf("\n"); while(!emptystack(topb))
} {
pop(b,&c,&topb);
printf("%c",c);
}
printf("\n");
}
QUEUES:
1. A FIFO structure (First-In-First-Out)
2. A person who has come first, will get served first.
3. Represented by a pipe open from both ends.
4. Has a ‘FRONT’ and a ‘REAR’ pointer
5. Service is given from FRONT.
6. New elements are added at the REAR.
7. FRONT points to the first filled location.
8. REAR points to the next to last filled position.
9. As in normal queue, elements move forward whenever an element is served.
10. Problem with above implementation ?
11. Algorithm for entering an element in the queue:
ENTERQ:
- Insert element at REAR Position. Make REAR=REAR+1
12. Algorithm for deleting an element from queue:
DELETEQ:
Remove the element from FRONT position. Make FRONT=FRONT+1
13. Problem with this implementation ?
14. How to solve this problem ?
QUEUE - IMPLEMENTATION 1: (SIZE OF ARRAY IS N)
1. LINEAR Q, FRONT ALWAYS AT INDEX 0; ELEMENTS MOVE FORWARD WHENEVER SERVICE IS GIVEN.
2. FRONT IS AT FIRST FILLED LOCATION(always at index 0) REAR IS AT NEXT TO LAST FILLED LOCATION.
3. INITIALLY FRONT=REAR=0. (INITQ). EMPTYQ IS WHEN REAR=0, FULLQ IS WHEN REAR=N
4. FUNCTIONS FOR THIS IMPLEMENTATION:
#include<stdio.h>
#define N 5 int emptyq(int r)
void initq(int *f,int *r) {
{ if (r==0)
*f=0; return 1;
*r=0;
}
else
void enterq(int *a,int x,int *r) return 0;
{ }
a[*r]=x; int fullq(int r)
*r=*r+1; {
} if (r==N)
void deleteq(int *a,int *x,int *r)
return 1;
{
int i; else
*x=a[0]; return 0;
for(i=0;i<*r;i++) }
a[i]=a[i+1];
*r=*r-1;
}
int main()
{
int a[N];
int front,rear;
int choice,v,i;
initq(&front,&rear);
do{
printf("1. Enter a value in queue\n");
printf("2. Delete a value from queue\n");
printf("3. Print queue values\n");
printf("4. EXIT\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Give value to enter: ");
scanf("%d",&v);
if (fullq(rear)==0)
enterq(a,v,&rear);
else
printf("queue full, cannot enter\n");
break;
case 2:
if (emptyq(rear)==0)
{
deleteq(a,&v,&rear);
printf("delete queue element :%d\n",v);
}
else
printf("queue empty, cannot delete\n");
break;
case 3:
printf("queue values are : ");
for(i=0;i<rear;i++)
printf("%d. %d,",i,a[i]);
printf("\n");
printf("current front= %d, current rear= %d\n",front,rear);
break;
case 4:
printf("Thank you, BYE\n");
break;
default:
printf("invalid choice, enter again\n");
}
} while(choice!=4);
}
QUEUE - IMPLEMENTATION 2: (SIZE OF ARRAY IS N)
1. LINEAR Q, FRONT ALSO MOVES FORWARD WHEN SERVICE IS GIVEN. ELEMENTS DON’T MOVE.
2. FRONT IS AT FIRST FILLED LOCATION, REAR IS AT NEXT TO LAST FILLED LOCATION.
3. INITIALLY FRONT=REAR=0. (INITQ). EMPTYQ IS WHEN FRONT==REAR, FULLQ IS WHEN REAR=N
4. FUNCTIONS FOR THIS IMPLEMENTATION:
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
int emptylist(int list)
{
if (list==-1)
return 1;
else
return 0;
}
int fulllist(int avail)
{
if (avail==-1)
return 1;
else
return 0;
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
int searchlist(node1 *a,int list,int x)
{
while(list!=-1 && a[list].info<x)
list=a[list].next;
if (a[list].info==x)
return 1;
else
return 0;
}
void traverselist(node1 *a,int list)
{
while(list!=-1)
{
printf("%d. %d--> ",list,a[list].info);
list=a[list].next;
}
printf("\n");
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
void enterlist(node1 *a,int *list,int *avail,int x)
{
int l,back,q;
l=*avail;
*avail=a[*avail].next;
a[l].info=x;
if (*list==-1 || x<a[*list].info)
{
a[l].next=*list;
*list=l;
}
else
{
back=*list;
q=*list;
while(q!=-1 && a[q].info<x)
{
back=q;
q=a[q].next;
}
a[back].next=l;
a[l].next=q;
}
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
void deletelist(node1 *a,int *list,int *avail,int x)
{
int l,back;
l=*list;
while (l!=-1 && a[l].info<x)
{
back=l;
l=a[l].next;
}
if (l!=-1 && a[l].info==x)
{
if (l==*list)
{
*list=a[l].next;
}
else
{
a[back].next=a[l].next;
}
a[l].next=*avail;
*avail=l;
}
else
printf("value not present in the list\n");
}
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
int main() case 2:
{ printf("Give value to delete: ");
node1 a[N]; scanf("%d",&v);
int list,avail; if (emptylist(list)==0)
int choice,v,i; deletelist(a,&list,&avail,v);
initlist(a,&list,&avail); else
do{ printf("list empty, cannot delete\n");
printf("1. Enter a value in list\n"); break;
printf("2. Delete a value from list\n"); case 3:
printf("3. Search in list\n"); printf("Give value to search: ");
printf("4. Traverse the list\n"); scanf("%d",&v);
printf("5. EXIT\n"); if (searchlist(a,list,v)==1)
printf("Enter your choice:"); printf("Value found in list\n");
scanf("%d",&choice); else
switch(choice) printf("value not found in list\n");
{ break;
case 1: case 4:
printf("Give value to enter: "); printf("The Avail(%d)-->",avail);
scanf("%d",&v); traverselist(a,avail);
if (fulllist(avail)==0) printf("\n");
enterlist(a,&list,&avail,v); printf("The List(%d)-->",list);
else traverselist(a,list);
printf("list full, cannot enter\n"); printf("\n");
break; break;
LINKED LIST (USING ARRAYS - STATIC MEMORY ALLOCATION):
case 5:
printf("Thank you, BYE\n");
break;
default:
printf("invalid choice, enter again\n");
}
} while(choice!=5);
}
FORMAT FOR MID-TERM EXAMINATION: