Professional Documents
Culture Documents
Pratheepan
ATI-Trincomalee
5
2. queue (first-in-first-out)
6
enQueue(6)
deQueue()
Data Structures:
Data structure is the arrangement of data in a computer memory/storage.
Data structure is the implementation of ADT such as stack, queue, linked list, tree, graph.
When working with certain data structures we need to know how to insert new data, how to search
for a specific item, and how to delete a specific item.
Data structure helps for efficient programming.
Data structure reduces complexity of the program and its calculations.
There are two types of data structures: Linear data structure and Nonlinear data structure
Linear and Nonlinear Data Structures:
Linear Data Structure
Data Organized Sequentially (one after the other)
Easy to implement because the computer memory is also
organized as linear fashion
E.g: Array, Stack, Queue, Linked List
What is Algorithm?
An algorithm is a step by step procedure for solving a problem in a finite amount of time.
Many algorithms apply directly to a specific data structures.
Efficiency of an Algorithm:
Some algorithms are more efficient than others. We would prefer to choose an efficient algorithm.
Running time of algorithms typically depends on the input set, and its size(n).
J.Pratheepan
ATI-Trincomalee
Big O Notation:
We can say that a function is of the order of n", which can be written as O(n) to describe the upper
bound on the number of operations. This is called Big-Oh notation.
Some common orders are:
O(1) constant (the size of n has no
effect)
O(log n) logarithmic
O(n log n)
O(n)
O(n2) quadratic
O(n3) cubic
O(2n) exponential
If the number of operations is 500, then the
big O notation is O(1).
If the number of operations is 9n + 200,
then the big O notation is O(n).
If the number of operations is n3 + 3n +
8,then the big O notation is O(n3).
Generally we can take O(1) is faster than
O(n) and O(n) is faster than O(n3).
Best, Worst and Average Cases:
Best case efficiency is the minimum number
of steps that an algorithm can take any
collection of data values.
Worst case efficiency is the maximum
number of steps that an algorithm can take for any collection of data values.
Average case efficiency
is the efficiency averaged on all possible inputs
must assume a distribution of the input
is normally assumed for uniform distribution (all keys are equally probable)
For example, when we search for an element in a list sequentially,
In best case, the cost is 1 compare. That is O(1).
In worst case, the cost is n compares. That is O(n).
In the average case, the cost may be (n+1)/2. That is O(n).
J.Pratheepan
ATI-Trincomalee
J.Pratheepan
ATI-Trincomalee
J.Pratheepan
ATI-Trincomalee
p = 9; *iptr = 5; *cptr=$;
cout<<&p; cout<<*iptr;cout<<*cptr;
2cdf
1a5c
iptr
9
1a5c
iptr
1a5c
$t#
2cdf
iptr = &p;
cout<<*iptr;
c75c
cptr
c75c
cptr
/0
cptr
/0
1a5c
iptr
2cdf
emp
empNo
salary
emp
empNo 1005
salary 40000.00
emp.empNo=1005;
emp.salary=40000.00;
cout<<emp.salary+1000.00;
employee* eptr;
eptr = new employee;
eptr->empNo=1006;
eptr->salary=30000.00;
cout<<eptr->empNo;
41000.00
eptr
empNo 1006
salary 30000.00
1006
head
next
data
next
data
next
data
next
data
E.g1:
7 /
next
data
next
data
next
data
next
head
front
rear
data
E.g2:
7 /
head
next
data
ATI-Trincomalee
previous
next
data
previous
next
data
previous
next
data
E.g3:
J.Pratheepan
7 /
2. insertFirstElt(5)
5 /
data
3.insertAtFront(3)
data
4. insertAtEnd(8)
head
data
next
newNode
next
5 /
data
head
next
head
newNode
next
head
next
1. initializeList()
8 /
head
newNode
next
8 /
7
data
next
data
next
data
5. insertAfter(5,7)
J.Pratheepan
ATI-Trincomalee
head
next
data
next
data
next
data
next
data
6. deleteElt(5):
8 /
J.Pratheepan
newNode->next=head;
head=newNode;
}
void LinkedList::insertAtEnd(int elt)
{
listNode *newNode, *curNode;
newNode=new listNode;
newNode->data=elt;
newNode->next=NULL;
if (!head)
head=newNode;
else
{
curNode=head;
while (curNode->next != NULL)
curNode = curNode->next;
curNode->next = newNode;
}
}
}
void LinkedList::deleteElt(int elt)
{
}
void LinkedList::displayList()
{
listNode* curNode;
curNode=head;
while (curNode)
{
cout<<curNode->data<<" ";
curNode=curNode->next;
}
}
int LinkedList::isEmpty()
{
if (head== NULL)
return 1;
else
return 0;
}
ATI-Trincomalee
J.Pratheepan
ATI-Trincomalee
J.Pratheepan
ATI-Trincomalee
2. p =isEmpty()
p = true
3. push(5)
4. push(7)
7
5
5. push(6)
6
7
5
10
J.Pratheepan
ATI-Trincomalee
6. q = isEmpty(); r = isFull();
6
7
5
q = false; r = false
7. x = pop()
x=6
7
5
8. y = topElt()
y=7
7
5
top = -1
4
3
2
1
0
top = -1
4
3
2
1
0
top = 0
4
3
2
1
5 0
top = 1
4
3
2
7 1
5 0
2. p =isEmpty()
p = true
3. push(5)
4. push(7)
11
J.Pratheepan
5. push(6)
top = 2
4
3
6 2
7 1
5 0
6. q = isEmpty(); r = isFull();
q = false; r = false
top = 2
7. x = pop()
x=6
top = 1
4
3
2
7 1
5 0
top = 1
4
3
2
7 1
5 0
8. y = topElt()
y=7
4
3
6 2
7 1
5 0
12
ATI-Trincomalee
J.Pratheepan
Stack::Stack()
{
top=(-1);
}
void Stack::initializeStack()
{
top=(-1);
}
void Stack::push(int elt)
{
if (top < STK_SIZE-1) stk[++top]=elt;
}
int Stack::pop()
{
if (top > -1)
return stk[top--];
else
return 999; //Some invalid integer should be returned
}
int Stack::topElt()
{
if (top > -1)
return stk[top];
else
return 999; //Some invalid integer should be returned
}
int Stack::isEmpty()
{
return (top == (-1));
}
int Stack::isFull()
{
return (top == (STK_SIZE-1));
}
void Stack::displayStack()
{
int i=top;
while (i>-1)
{
cout<<stk[i]<<endl;
i--;
}
}
void main()
{
clrscr();
Stack s;
s.push(5);
s.push(7);
s.push(6);
13
ATI-Trincomalee
J.Pratheepan
ATI-Trincomalee
int x=s.pop();
s.push(9);
s.displayStack();
}
Output:
9
7
5
Dynamic (Linked List based) Implementation of Stack Operations:
initializeStack() - top=NULL; //Similar to initialzeList() and it is better to use top instead of head.
push() - newNode->next=top; top=newNode; //Similar to insertAtFront()
pop() - x=top->data; top=top->next; return x;
topElt() - return top->data
isEmpty() - if (top==NULL) return 1 else return 0
isFull() - return 0; //Always return false
displayStack() - Similar to displayList()
Advantages of Stack:
Last-in-first-out access
Disadvantages of Stack:
Difficult to access other items
14
J.Pratheepan
ATI-Trincomalee
4. enQueue(9)
enQueue(7)
7. q = isFull()
q = false
8. enQueue(3)
5. x=deQueue()
x=5
6. enQueue(2)
enQueue(6)
9. r = isFull()
y = deQueue()
r = true
y=9
1. initializeQueue()
front
rear
size
-1
-1
0
15
J.Pratheepan
ATI-Trincomalee
1
9
2
7
1
9
2
7
1
9
2
7
3
2
4
6
1
9
2
7
3
2
4
6
1
9
2
7
3
2
4
6
2. p=isEmpty()
p = true
front
rear
size
-1
-1
0
0
5
3. enQueue(5)
front
rear
size
-1
0
1
0
5
4. enQueue(9)
enQueue(7)
front
rear
size
-1
2
3
0
5. x=deQueue()
x=5
front
rear
size
0
2
2
0
6. enQueue(2)
enQueue(6)
front
rear
size
0
4
4
0
7. q = isFull()
q = false
front
rear
size
0
4
4
0
3
8. enQueue(3)
front
rear
0
0
16
J.Pratheepan
r = true
y=9
5
0
3
9. r = isFull()
y = deQueue()
front
rear
size
ATI-Trincomalee
2
7
3
2
1
0
4
17
4
6
J.Pratheepan
ATI-Trincomalee
int Queue::deQueue()
{
if (size > 0)
{
front=(front+1)%Q_SIZE;
size--;
return que[front];
}
else
return 999; //Some invalid integer should be returned or cout<<Queue is empty
}
int Queue::frontElt()
{
if (size>0)
{
return que[(front+1)%Q_SIZE];
}
else
return 999; //Some invalid integer should be returned or cout<<Queue is empty
}
int Queue::isEmpty()
{
return (size == 0);
}
int Queue::isFull()
{
return (size == Q_SIZE);
}
void Queue::displayQueue()
{
int i=front;
for (int j=1;j<=size;j++)
{
i=(i+1)%Q_SIZE;
cout<<que[i]<<endl;
}
}
void main()
{
clrscr();
Queue q;
q.enQueue(5);
q.enQueue(9);
q.enQueue(7);
int x=q.deQueue();
q.enQueue(2);
q.enQueue(6);
q.enQueue(3);
int y=q.deQueue();
q.displayQueue();
}
Output: 7 2 6 3
18
J.Pratheepan
ATI-Trincomalee
19
J.Pratheepan
ATI-Trincomalee
Size=9 Height=4
3. Degree
The degree of a node is the number of its children.
The degree of a tree is the maximum degree of any of its nodes.
20
J.Pratheepan
ATI-Trincomalee
4. Path
Path between two nodes in a tree is a sequence of edges which connect those nodes.
21
J.Pratheepan
8
/
22
ATI-Trincomalee
J.Pratheepan
ATI-Trincomalee
7. Sorting Algorithms
What is sorting?
Arranging items in ascending or descending order is called as sorting.
There are different types of sorting techniques each of which is good for some cases such as nearly
sorted, reversed, random, etc.
Selection Sort Algorithm:
Here we repeatedly find the next largest (or smallest) element in the array and move it to its final
position in the sorted array.
Example: Sort the numbers 6, 7,72, 4, 32, 65, 9, 56 using selection sort.
0
1
2
3
4
5
6
7
Original
Pass0
7
72
4
32
65
9
56
6
Pass1
72
32
65
56
Pass2
72
32
65
56
Pass3
72
32
65
56
Pass4
32
65
72
56
Pass5
32
65
72
56
Pass6
32
56
72
65
Pass7
32
56
65
72
Pseudo Code:
swap(x, y)
t=x
x=y
y=t
selectionSort (a[],n) //Let a be an array containing n items
for i = 0 to n-2
m=i
for j = i+1 to n-1
if (a[j] < a[m]) m = j
next j
swap(a[i],a[m])
next i
C++ Code:
#include<iostream.h>
#include<conio.h>
void displayArray(int *a, int n)
{
int i;
for (i=0; i<n; i++)
cout<<a[i]<<" ";
}
23
Sorted
J.Pratheepan
ATI-Trincomalee
32
65
56
72
Pass2
32
56
65
72
Pass3
32
56
65
72
Pass4
32
56
65
72
Pass5
32
56
65
72
Pseudo Code:
bubbleSort(a[],n) //Let a be an array containing n items
max = n-2
swapped = true
while (max>0 AND swapped=true)
swapped = false
for j = 0 to max
24
Original
Sorted
J.Pratheepan
ATI-Trincomalee
25
J.Pratheepan
ATI-Trincomalee
8. Searching Algorithm
What is search algorithm?
A search algorithm is an algorithm for finding an item among a collection of items.
Sequential/Linear Search Algorithm:
It examines the first element in the list and then second element and so on until a much is found.
Pseudo code:
int sequentialSearch(a[],n,t) //It returns the location of the target t in the array a[] with n elements.
for i = 0 to n-1
if (a[i]=t) return i;
next i
return -1;
C++ Implementation:
#include<iostream.h>
#include<conio.h>
int sequentialSearch(int *a, int n, int t)
{
int i;
for (i = 0; i < n; i++)
if (a[i]==t) return i;
return (-1);
}
void main ()
{
clrscr();
int num[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = 10;
int t = 99;
cout<<t<<" is found at "<<sequentialSearch(num, n, t)<<".";
}
Output: 99 is found at 5.
Binary Search Algorithm:
Here the elements should be in (ascending) order and the elements should be saved in a randomly
accessible data structure like array.
The basic algorithm is to find the middle element of the list, compare it against the key/target, decide
which half of the list must contain the key, and repeat with that half.
Pseudo code:
int binarySearch(a[],l,u,t) //It returns the location of t in the array a[] from the index l to u.
p = ( l + u) / 2;
while(a[p] t AND l<=u)
if (a[p] > t)
u=p-1
else
l=p+1
p = (l + u) / 2
end while
if (l <= u)
return p
else
return -1
26
J.Pratheepan
ATI-Trincomalee
C++ Implementation:
int binarySearch(int* a, int l, int u, int t)
{
int p;
p = ( l + u) / 2;
while((a[p] != t) && (l<=u))
{
if (a[p] > t)
u = p - 1;
else
l = p + 1;
p = (l + u) / 2;
}
if (l <= u)
return p;
else
return (-1);
}
void main ()
{
clrscr();
int num[] = {1, 2, 7, 9, 50, 99, 100, 150, 190, 200};
int n = 10;
int t = 99;
cout<<t<<" is found at "<<binarySearch(num, 0, n-1, t)<<".";
}
Output: 99 is found at 5.
Recursive Pseudo Code:
int recBinarySearch(a[],l,u,t) //It returns the location of t in the array a[] from the index l to u.
if l>u then
return -1
else
mid=(l+u)/2
if t=a[mid] then
return mid
else if t<a[mid] then
return recBinarySearch(a[],l,mid-1,t)
else
return recBinarySearch(a[],mid+1,u,t)
end if
end if
Recursive C++ Code:
int recBinarySearch(int* a, int l, int u, int t)
{
int mid;
if (l>u)
return (-1);
else
{
mid=(l+u)/2;
if (t==a[mid])
return mid;
else if (t<a[mid])
return recBinarySearch(a,l,mid-1,t);
27
J.Pratheepan
ATI-Trincomalee
else
return recBinarySearch(a,mid+1,u,t);
}
}
void main ()
{
clrscr();
int a[] = {1, 2, 7, 9, 50, 99, 100, 150, 190, 200};
int n = 10;
int t = 99;
cout<<t<<" is found at "<<recBinarySearch(a, 0, n-1, t)<<".";
}
Output: 99 is found at 5.
Efficiency of the Search Algorithms (Best, Worst and Average Cases):
Searching Technique Best case Average Case Worst Case
Sequential Search
O(1)
O(n)
O(n)
Binary Search
O(1)
O (log n)
O(log n)
The difference between O(log(N)) and O(N) is extremely significant when N is large.
For example, suppose your array contains 2 billion values, the sequential search would involve
about a billion comparisons; binary search would require only 32 comparisons!
28